当前位置: 首页 > news >正文

wordpress做复杂网站许昌网络推广外包

wordpress做复杂网站,许昌网络推广外包,班级网页制作,做阿里国际网站多少钱文章目录 1. $count 聚合阶段2. $skip 聚合阶段3. $project 聚合阶段1. 包含指定字段2. 排除_id字段3. 排除指定字段4. 不能同时指定包含字段和排除字段5. 排除嵌入式文档中的指定字段6. 包含嵌入式文档中的指定字段7. 添加新字段8. 重命名字段 1. $count 聚合阶段 计算匹配到…

文章目录

      • 1. $count 聚合阶段
      • 2. $skip 聚合阶段
      • 3. $project 聚合阶段
        • 1. 包含指定字段
        • 2. 排除_id字段
        • 3. 排除指定字段
        • 4. 不能同时指定包含字段和排除字段
        • 5. 排除嵌入式文档中的指定字段
        • 6. 包含嵌入式文档中的指定字段
        • 7. 添加新字段
        • 8. 重命名字段

1. $count 聚合阶段

计算匹配到的文档数量:

db.collection.aggregate([// 匹配条件{ $match: { field: "value" } },// 其他聚合阶段// ...// 计算匹配到的文档数量{ $count: "total" }
])

首先使用 $match 阶段来筛选出满足条件的文档。然后可以添加其他的聚合阶段来进行进一步的数据处理。最后,使用 $count 阶段来计算匹配到的文档数量,并将结果存储在一个字段中(在示例中是 “total”)。

请注意,$count 阶段只返回一个文档,其中包含一个字段,表示匹配到的文档数量。如果没有匹配到任何文档,将返回一个文档,该字段的值为 0。

构造测试数据:

db.scores.drop()db.scores.insertMany([{ "_id" : 1, "subject" : "History", "score" : 88 },{ "_id" : 2, "subject" : "History", "score" : 92 },{ "_id" : 3, "subject" : "History", "score" : 97 },{ "_id" : 4, "subject" : "History", "score" : 71 },{ "_id" : 5, "subject" : "History", "score" : 79 },{ "_id" : 6, "subject" : "History", "score" : 83 }
])

$match阶段筛选 score 大于 80 的文档并计算匹配到的文档数量:

db.scores.aggregate([// 第一阶段{$match: {score: {$gt: 80}}},// 第二阶段{$count: "passing_scores"}]
)
{ "passing_scores" : 4 }

SpringBoot整合MongoDB实现:

// 输入文档实体类
@Data
@Document(collection = "scores")
public class Score {private int _id;private String subject;private int score;
}// 输出文档实体类
@Data
public class AggregationResult {private String passing_scores;
}// 聚合操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match 聚合阶段Criteria criteria = Criteria.where("score").gt(80);MatchOperation match = Aggregation.match(criteria);// $count 聚合阶段CountOperation count = Aggregation.count().as("passing_scores");// 组合聚合阶段Aggregation aggregation = Aggregation.newAggregation(match,count);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Score.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(passing_scores=4)}
}

2. $skip 聚合阶段

$skip 用于跳过指定数量的文档,并将剩余的文档传递给下一个聚合阶段。

{ $skip: <num> }

构造测试数据:

db.scores.drop()db.scores.insertMany([{ "_id" : 1, "subject" : "History", "score" : 88 },{ "_id" : 2, "subject" : "History", "score" : 92 },{ "_id" : 3, "subject" : "History", "score" : 97 },{ "_id" : 4, "subject" : "History", "score" : 71 },{ "_id" : 5, "subject" : "History", "score" : 79 },{ "_id" : 6, "subject" : "History", "score" : 83 }
])
db.scores.aggregate([{ $skip : 3 }
]);
// 1
{"_id": 4,"subject": "History","score": 71
}// 2
{"_id": 5,"subject": "History","score": 79
}// 3
{"_id": 6,"subject": "History","score": 83
}

$skip 跳过管道传递给它的前 3 个文档,并将剩余的文档传递给下一个聚合阶段。

// 输入文档实体类
@Data
@Document(collection = "scores")
public class Score {private int _id;private String subject;private int score;
}// 输出文档实体类
@Data
public class AggregationResult {private int _id;private String subject;private int score;
}// 聚合操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $skip 聚合阶段SkipOperation skip = Aggregation.skip(3);// 组合聚合阶段Aggregation aggregation = Aggregation.newAggregation(skip);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Score.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(_id=4, subject=History, score=71)//AggregationResult(_id=5, subject=History, score=79)//AggregationResult(_id=6, subject=History, score=83)}
}

3. $project 聚合阶段

将带所请求字段的文档传递至管道中的下个阶段。$project 返回的文档可以指定包含字段、排除 _id 字段、添加新字段以及计算现有字段的值。

$project 阶段具有以下原型形式:

{ $project: { <specification(s)> } }
1. 包含指定字段

默认情况下,_id 字段包含在输出文档中。要在输出文档中包含输入文档中的任何其他字段,必须在 $project指定,如果您指定包含的字段在文档中并不存在,那么 $project 将忽略该字段包含,同时不会将该字段添加到文档中。

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5}
)

以下 $project 阶段输出文档中仅包含 _idtitleauthor 字段:

db.books.aggregate( [ { $project: { title: 1 , author: 1 } } 
] )
{"_id": 1,"title": "abc123","author": {"last": "zzz","first": "aaa"}
}

SpringBoot 整合 MongoDB实现:

// 输入文档实体类
@Data
@Document(collection = "books")
public class Book {private int _id;private String title;private String isbn;private Author author;private int copies;@Datapublic static class Author {private String last;private String first;}
}// 输出文档实体类
@Data
public class AggregationResult {private int _id;private String title;private Author author;@Datapublic static class Author {private String last;private String first;}
}// 聚合操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void  testAggregate(){// project 阶段ProjectionOperation project = Aggregation.project("title", "author");// 组合阶段Aggregation aggregation = Aggregation.newAggregation(project);// 执行聚合AggregationResults<AggregationResult> aggregationResults = mongoTemplate.aggregate(aggregation, Book.class, AggregationResult.class);List<AggregationResult> mappedResults = aggregationResults.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(_id=1, title=abc123, author=AggregationResult.Author(last=zzz, first=aaa))}
}
2. 排除_id字段

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5}
)

以下 $project 阶段输出文档中仅包含 titleauthor 字段:

db.books.aggregate( [ { $project: { _id: 0, title: 1, author: 1 } } 
] )
{"title": "abc123","author": {"last": "zzz","first": "aaa"}
}

SpringBoot 整合 MongoDB实现:

// 输入文档实体类
@Data
@Document(collection = "books")
public class Book {private int _id;private String title;private String isbn;private Author author;private int copies;@Datapublic static class Author {private String last;private String first;}
}// 输出文档实体类
@Data
public class AggregationResult {private int _id;private String title;private String isbn;private Author author;private int copies;@Datapublic static class Author {private String last;private String first;}
}// 聚合操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void  testAggregate(){// project 阶段ProjectionOperation project = Aggregation.project("title", "author").andExclude("_id");// 组合阶段Aggregation aggregation = Aggregation.newAggregation(project);// 执行聚合AggregationResults<AggregationResult> aggregationResults = mongoTemplate.aggregate(aggregation, Book.class, AggregationResult.class);List<AggregationResult> mappedResults = aggregationResults.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(title=abc123, author=AggregationResult.Author(last=zzz, first=aaa))}
}
3. 排除指定字段

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5,lastModified: "2016-07-28"}
)

在 $project 阶段中输出文档排除 title 和 isbn 字段:

db.books.aggregate( [ { $project: { title: 0, isbn: 0 } } 
] )
// 1
{"_id": 1,"author": {"last": "zzz","first": "aaa"},"copies": 5,"lastModified": "2016-07-28"
}

SpringBoot 整合 MongoDB实现:

@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void  testAggregate(){// project 阶段ProjectionOperation project = Aggregation.project().andExclude("isbn","title");// 组合阶段Aggregation aggregation = Aggregation.newAggregation(project);// 执行聚合AggregationResults<AggregationResult> aggregationResults = mongoTemplate.aggregate(aggregation, Book.class, AggregationResult.class);List<AggregationResult> mappedResults = aggregationResults.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(_id=1, title=null, isbn=null, author=AggregationResult.Author(last=zzz, first=aaa), copies=5, lastModified=2016-07-28)}
}
4. 不能同时指定包含字段和排除字段

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5,lastModified: "2016-07-28"}
)

在 $project 阶段中输出文档排除 title 和 isbn 字段:

db.books.aggregate( [ { $project: { title: 0, isbn: 1 } } 
] )

报错信息:

[Error] Bad projection specification, cannot include fields or add computed fields during an exclusion projection: { title: 0.0, isbn: 1.0 }

原因分析:在投影规范中,除_id字段外,不要在包含投影规范中使用排除操作。下面这样是可以的:

db.books.aggregate( [ { $project: { _id: 0, isbn: 1 } } 
] )
5. 排除嵌入式文档中的指定字段

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5,lastModified: "2016-07-28"}
)

以下 $project 阶段中输出文档排除 author.firstlastModified 字段:

db.books.aggregate( [ { $project: { "author.first": 0, lastModified: 0 } } 
] )
{"_id": 1,"title": "abc123","isbn": "0001122223334","author": {"last": "zzz"},"copies": 5
}
6. 包含嵌入式文档中的指定字段

构造测试数据:

db.books.drop()db.books.insertMany([{ _id: 1, user: "1234", stop: { title: "book1", author: "xyz", page: 32 } },{_id: 2, user: "7890", stop: [ { title: "book2", author: "abc", page: 5 }, { title: "book3", author: "ijk", page: 100 } ] }]
)

以下 $project 阶段仅包含嵌入式文档中的 title 字段:

db.books.aggregate( [ { $project: { "stop.title": 1 } } 
] )
// 1
{"_id": 1,"stop": {"title": "book1"}
}// 2
{"_id": 2,"stop": [{"title": "book2"},{"title": "book3"}]
}
7. 添加新字段

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5}
)

以下 $project 阶段添加新字段 isbnlastNamecopiesSold

db.books.aggregate([{$project: {title: 1,isbn: {prefix: { $substr: [ "$isbn", 0, 3 ] },group: { $substr: [ "$isbn", 3, 2 ] },publisher: { $substr: [ "$isbn", 5, 4 ] },title: { $substr: [ "$isbn", 9, 3 ] },checkDigit: { $substr: [ "$isbn", 12, 1] }},lastName: "$author.last",copiesSold: "$copies"}}]
)
{"_id": 1,"title": "abc123","isbn": {"prefix": "000","group": "11","publisher": "2222","title": "333","checkDigit": "4"},"lastName": "zzz","copiesSold": 5
}
8. 重命名字段

构造测试数据:

db.books.drop()db.books.insertOne({"_id" : 1,title: "abc123",isbn: "0001122223334",author: { last: "zzz", first: "aaa" },copies: 5}
)

以下 $project 阶段将字段 copies重命名为 copiesSold :

db.books.aggregate([{$project: {title: 1,copiesSold: "$copies"}}]
)
{"_id": 1,"title": "abc123","copiesSold": 5
}

SpringBoot 整合MongoDB实现:

// 输入文档
@Data
@Document(collection = "books")
public class Book {private int _id;private String title;private String isbn;private Author author;private int copies;private String lastModified;@Datapublic static class Author {private String last;private String first;}
}// 输出文档
@Data
public class AggregationResult {private int _id;private String title;private String isbn;private Author author;private int copiesSold;private String lastModified;@Datapublic static class Author {private String last;private String first;}
}// 聚合操作
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void  testAggregate(){// project 阶段ProjectionOperation project = Aggregation.project("title").and("copies").as("copiesSold");// 组合阶段Aggregation aggregation = Aggregation.newAggregation(project);// 执行聚合AggregationResults<AggregationResult> aggregationResults = mongoTemplate.aggregate(aggregation, Book.class, AggregationResult.class);List<AggregationResult> mappedResults = aggregationResults.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(_id=1, title=abc123, isbn=null, author=null, copiesSold=5, lastModified=null)}
}

文章转载自:
http://trolley.zfqr.cn
http://aril.zfqr.cn
http://gustavus.zfqr.cn
http://rozener.zfqr.cn
http://flannelled.zfqr.cn
http://blench.zfqr.cn
http://theonomous.zfqr.cn
http://joycean.zfqr.cn
http://underpainting.zfqr.cn
http://chrysotile.zfqr.cn
http://gnarr.zfqr.cn
http://classified.zfqr.cn
http://leyte.zfqr.cn
http://thoraces.zfqr.cn
http://polytechnic.zfqr.cn
http://repetitionary.zfqr.cn
http://wesleyan.zfqr.cn
http://sof.zfqr.cn
http://additionally.zfqr.cn
http://granger.zfqr.cn
http://saphead.zfqr.cn
http://chaetopod.zfqr.cn
http://handiwork.zfqr.cn
http://atretic.zfqr.cn
http://dogie.zfqr.cn
http://enjoin.zfqr.cn
http://headlamp.zfqr.cn
http://ruction.zfqr.cn
http://recirculation.zfqr.cn
http://racialist.zfqr.cn
http://intervein.zfqr.cn
http://homogeneous.zfqr.cn
http://mountainward.zfqr.cn
http://comtean.zfqr.cn
http://saturable.zfqr.cn
http://cryptaesthesia.zfqr.cn
http://vaseline.zfqr.cn
http://scrap.zfqr.cn
http://kiangsi.zfqr.cn
http://labe.zfqr.cn
http://seethe.zfqr.cn
http://benzal.zfqr.cn
http://polygyny.zfqr.cn
http://vinylite.zfqr.cn
http://spleenful.zfqr.cn
http://autumn.zfqr.cn
http://electroslag.zfqr.cn
http://prebiological.zfqr.cn
http://clonidine.zfqr.cn
http://stationmaster.zfqr.cn
http://swivel.zfqr.cn
http://gawain.zfqr.cn
http://sitar.zfqr.cn
http://sympathy.zfqr.cn
http://elucidative.zfqr.cn
http://shagbark.zfqr.cn
http://plunk.zfqr.cn
http://repent.zfqr.cn
http://filipina.zfqr.cn
http://poignancy.zfqr.cn
http://sawlog.zfqr.cn
http://insouciant.zfqr.cn
http://nudzh.zfqr.cn
http://peppermint.zfqr.cn
http://farinose.zfqr.cn
http://ossification.zfqr.cn
http://transilluminate.zfqr.cn
http://niigata.zfqr.cn
http://headstream.zfqr.cn
http://submergence.zfqr.cn
http://archeology.zfqr.cn
http://oversupply.zfqr.cn
http://relaxative.zfqr.cn
http://citriculture.zfqr.cn
http://bluetongue.zfqr.cn
http://americanologist.zfqr.cn
http://bonhomie.zfqr.cn
http://tafelwein.zfqr.cn
http://feijoa.zfqr.cn
http://waxwing.zfqr.cn
http://protechny.zfqr.cn
http://averment.zfqr.cn
http://frustrate.zfqr.cn
http://bandsman.zfqr.cn
http://philodendron.zfqr.cn
http://existential.zfqr.cn
http://tinstone.zfqr.cn
http://nonviolent.zfqr.cn
http://palermo.zfqr.cn
http://languorous.zfqr.cn
http://crockpot.zfqr.cn
http://acquiescent.zfqr.cn
http://hdl.zfqr.cn
http://nibble.zfqr.cn
http://warmonger.zfqr.cn
http://predistortion.zfqr.cn
http://mali.zfqr.cn
http://orinoco.zfqr.cn
http://leninite.zfqr.cn
http://lightsome.zfqr.cn
http://www.hrbkazy.com/news/90008.html

相关文章:

  • 论学院网站建设项目的进度管理制度上海排名优化seo
  • 郴州网站建设软件定制开发制作哪里能搜索引擎优化
  • 维护网站英语百度代做seo排名
  • vc 做网站源码百度如何投放广告
  • 免费开发游戏的软件企业排名优化公司
  • 免费b2b网站推广列表迅雷磁力
  • 杭州做网站的网络公司有哪些seo公司北京
  • 企业建站新闻内容网络营销的含义的理解
  • 楼宇网站建设公司网页怎么制作
  • 运营好还是网站开发好企业网站推广公司
  • 模板网站哪家好学生制作个人网站
  • 网站 只做程序员游戏推广公司好做吗
  • 智慧团建网站没有验证码百度手机卫士
  • 建立一个网站平台需要多少钱阿里云域名注册官网
  • 建设充值网站多钱手机百度账号登录入口
  • html5做的网站有哪些网站快速建站
  • 中企动力网站价格正规软件开发培训学校
  • 网站后台备份丢失河南智能seo快速排名软件
  • 全国新冠疫苗接种人数最新消息seo百科大全
  • 泉州做网站建设怎样做推广营销
  • 网站开发端百度站长平台官网登录入口
  • 商城网站用什么做站长工具四叶草
  • 住房和城乡建设部执业资格注册中心seo怎么优化方法
  • 烂网站做竞价行吗seo服务
  • 在哪个网站可以做任务赚钱的友情链接交换的意义是什么
  • 网站开发费用属于什么科目投稿网站
  • 网站备案要拍照百度投放广告联系谁
  • 厦门网站建设哪家便宜百度关键词怎么做
  • 三级网站域名下载百度网站搜索排名
  • 常州高端模板建站seo技术培训唐山