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

新乡专业做淘宝网站去除痘痘怎么有效果

新乡专业做淘宝网站,去除痘痘怎么有效果,网站开发翻译插件,建个营销型网站多少钱文章目录 1. $match 聚合阶段1. 构造测试数据2. $match 示例3. $match 示例 2. $sort 聚合阶段1. 排序一致性问题2. $sort 示例 3. $limit 聚合阶段 1. $match 聚合阶段 $match 接受一个指定查询条件的文档。 $match 阶段语法&#xff1a; { $match: { <query> } }$ma…

文章目录

    • 1. $match 聚合阶段
      • 1. 构造测试数据
      • 2. $match 示例
      • 3. $match 示例
    • 2. $sort 聚合阶段
      • 1. 排序一致性问题
      • 2. $sort 示例
    • 3. $limit 聚合阶段

1. $match 聚合阶段

$match 接受一个指定查询条件的文档。

$match 阶段语法:

{ $match: { <query> } }

$match 查询语法与读取操作查询语法相同,即 $match 不接受原始聚合表达式。要在 $match 中包含聚合表达式,请使用 $expr 查询表达式:

{ $match: { $expr: { <aggregation expression> } } }

尽可能早地将 $match 放在聚合管道中,由于 $match 限制了聚合管道中的文档总数,因此早期的 $match 操作会最大限度地减少管道中的处理量。

1. 构造测试数据

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 60,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])

2. $match 示例

使用 $match 来执行简易等值匹配,$match 会选择 author 字段等于 dave 的文档,而聚合返回以下内容:

db.articles.aggregate([ { $match : { author : "dave" } } ]
);
// 1
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 2
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}

SpringBoot 整合 MongoDB 实现:

@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match阶段Criteria criteria = Criteria.where("author").is("dave");MatchOperation match = Aggregation.match(criteria);Aggregation aggregation = Aggregation.newAggregation(match);// 执行聚合管道操作AggregationResults<Article> results= mongoTemplate.aggregate(aggregation, Article.class, Article.class);List<Article> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//Article(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//Article(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)}
}

这里输出文档直接使用了Article.class,可以重新定义实体类接收输出文档的字段:

@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $match阶段Criteria criteria = Criteria.where("author").is("dave");MatchOperation match = Aggregation.match(criteria);Aggregation aggregation = Aggregation.newAggregation(match);// 执行聚合管道操作AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)}
}

3. $match 示例

使用 $match 管道操作符选择要处理的文档,然后将结果导入到 $group 管道操作符,以计算文档的数量:

db.articles.aggregate( [// 第一阶段{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },// 第二阶段{ $group: { _id: null, count: { $sum: 1 } } }
] );

第一阶段:

$match 阶段选择 score 大于 70 但小于 90views 大于或等于 1000 的文档。

第二阶段:

m a t c h 阶段筛选的文档通过管道传送到 ‘ match 阶段筛选的文档通过管道传送到 ` match阶段筛选的文档通过管道传送到group` 阶段进行计数。

// 1
{"_id": null,"count": 5
}

SpringBoot 整合 MongoDB 实现:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}
@Data
public class AggregationResult {private String id;private Integer count;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// 第一阶段Criteria criteria = new Criteria();criteria.orOperator(Criteria.where("score").gt(70).lt(90), Criteria.where("views").gte(1000));MatchOperation match = Aggregation.match(criteria);// 第二阶段GroupOperation group = Aggregation.group().count().as("count");// 组合上面的2个阶段Aggregation aggregation = Aggregation.newAggregation(match,group);// 执行聚合管道操作AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);// AggregationResult(id=null, count=5)}
}

2. $sort 聚合阶段

$sort 将所有输入文档进行排序,然后按照排序将其返回至管道。

{ $sort: { <field1>: <sort order>, <field2>: <sort order> ... } }

$sort 接受排序依据的字段及相应排序顺序的文档。当 sort order=1时升序排序,sort order=-1 降序排序。

如果对多个字段进行排序,则按从左到右的顺序进行排序。例如,在上面的表单中,文档首先按 field1 排序。然后,具有相同 field1 值的文档将按 field2 进一步排序。

1. 排序一致性问题

MongoDB 不按特定顺序将文档存储在集合中。对包含重复值的字段进行排序时,可能会以任何顺序返回包含这些值的文档。如果需要一致的排序顺序,请在排序中至少纳入一个包含唯一值的字段。

db.restaurants.drop()db.restaurants.insertMany( [{ "_id" : 1, "name" : "Central Park Cafe", "borough" : "Manhattan"},{ "_id" : 2, "name" : "Rock A Feller Bar and Grill", "borough" : "Queens"},{ "_id" : 3, "name" : "Empire State Pub", "borough" : "Brooklyn"},{ "_id" : 4, "name" : "Stan's Pizzaria", "borough" : "Manhattan"},{ "_id" : 5, "name" : "Jane's Deli", "borough" : "Brooklyn"},
] )

以下命令使用 $sort 阶段对 borough 字段进行排序:

db.restaurants.aggregate([{ $sort : { borough : 1 } }]
)

在此示例中,排序顺序可能不一致,因为 borough 字段包含 ManhattanBrooklyn 的重复值。文档按 borough 的字母顺序返回,但具有 borough 重复值的文档的顺序在多次执行同一排序中可能不相同。

要实现一致的排序,可以在排序中添加一个仅包含唯一值的字段。以下命令使用 $sort 阶段对 borough 字段和 _id 字段进行排序:

db.restaurants.aggregate([{ $sort : { borough : 1, _id: 1 } }]
)

由于 _id 字段始终保证包含唯一值,因此在同一排序的多次执行中返回的排序顺序将始终相同。

2. $sort 示例

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])

对于要作为排序依据的一个或多个字段,可以将排序顺序设置为 1-1 以分别指定升序或降序。

db.articles.aggregate([{ $sort : { score : -1, views: 1 } }]
)
// 1
{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000
}// 2
{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999
}// 3
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}// 4
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 5
{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000
}// 6
{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50
}// 7
{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000
}

SpringBoot 整合 MongoDB:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $sort阶段SortOperation sortOperation = Aggregation.sort(Sort.by(Sort.Direction.DESC, "score")).and(Sort.by(Sort.Direction.ASC, "views"));Aggregation aggregation = Aggregation.newAggregation(sortOperation);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=55f5a1d3d4bede9ac365b25b, author=ty, score=95, views=1000)//AggregationResult(id=55f5a1d3d4bede9ac365b25a, author=li, score=94, views=999)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=55f5a192d4bede9ac365b257, author=ahn, score=60, views=1000)//AggregationResult(id=55f5a1d3d4bede9ac365b259, author=annT, score=55, views=50)//AggregationResult(id=55f5a192d4bede9ac365b258, author=li, score=55, views=5000)}
}

3. $limit 聚合阶段

$limit 聚合阶段限制传递至管道。$limit 取一个正整数,用于指定传递的最大文档数量:

{ $limit: <positive 64-bit integer> }

如果将 $limit 阶段与以下任何一项一起使用:

  • $sort 聚合阶段
  • sort() 方法
  • sort 命令

在将结果传递到$limit阶段之前,请务必在排序中至少包含一个包含唯一值的字段。

db.articles.drop()db.articles.insertMany([{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100},{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521},{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000},{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000},{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 60,"views": 50},{"_id": ObjectId("55f5a1d3d4bede9ac365b25a"),"author": "li","score": 94,"views": 999},{"_id": ObjectId("55f5a1d3d4bede9ac365b25b"),"author": "ty","score": 95,"views": 1000}
])
db.articles.aggregate([{ $limit : 5 }
]);
// 1
{"_id": ObjectId("512bc95fe835e68f199c8686"),"author": "dave","score": 80,"views": 100
}// 2
{"_id": ObjectId("512bc962e835e68f199c8687"),"author": "dave","score": 85,"views": 521
}// 3
{"_id": ObjectId("55f5a192d4bede9ac365b257"),"author": "ahn","score": 60,"views": 1000
}// 4
{"_id": ObjectId("55f5a192d4bede9ac365b258"),"author": "li","score": 55,"views": 5000
}// 5
{"_id": ObjectId("55f5a1d3d4bede9ac365b259"),"author": "annT","score": 55,"views": 50
}

SpringBoot 整合 MongoDB:

@Data
@Document(collection = "articles")
public class Article {@Idprivate String id;private String author;private int score;private int views;
}@Data
public class AggregationResult {@Idprivate String id;private String author;private int score;private int views;
}
@SpringBootTest
@RunWith(SpringRunner.class)
public class BeanLoadServiceTest {@Autowiredprivate MongoTemplate mongoTemplate;@Testpublic void aggregateTest() {// $sort阶段LimitOperation limitOperation = Aggregation.limit(5);Aggregation aggregation = Aggregation.newAggregation(limitOperation);// 执行聚合查询AggregationResults<AggregationResult> results= mongoTemplate.aggregate(aggregation, Article.class, AggregationResult.class);List<AggregationResult> mappedResults = results.getMappedResults();// 打印结果mappedResults.forEach(System.out::println);//AggregationResult(id=512bc95fe835e68f199c8686, author=dave, score=80, views=100)//AggregationResult(id=512bc962e835e68f199c8687, author=dave, score=85, views=521)//AggregationResult(id=55f5a192d4bede9ac365b257, author=ahn, score=60, views=1000)//AggregationResult(id=55f5a192d4bede9ac365b258, author=li, score=55, views=5000)//AggregationResult(id=55f5a1d3d4bede9ac365b259, author=annT, score=55, views=50)}
}
http://www.ds6.com.cn/news/4058.html

相关文章:

  • 网站备案怎么做抖音指数查询
  • 城市绿化建设英文网站seo网络推广是什么意思
  • 优秀的电商设计网站合肥网站推广公司
  • 学校门户网站购买模板建站
  • 石家庄信息门户网站定制会计培训班初级费用
  • 北京微信网站建设电话抖音广告怎么投放
  • wordpress广告插件网站点击排名优化
  • 网站的首页怎么做如何在互联网推广自己的产品
  • 网站被攻击空间关了怎么办微信营销的方法
  • 哈尔滨公司做网站住房和城乡建设部
  • 淘宝上做网站的信得过吗产品软文是什么
  • 手机网页怎么做出来的重庆seo网络优化师
  • 商业网站建设规划范文网络推广学校
  • 想学做网站从哪里入手如何写营销软文
  • 网站开发需求表百度怎么发广告
  • 宝鸡做网站电话营销心得体会感悟300字
  • 小型企业网站开发公司网站排名优化培训课程
  • 淘宝客网站开发制作网站的软件有哪些
  • 广州荔湾网站建设星力游戏源码
  • 厦门网站排名中国新闻发布
  • 做整体衣柜宣传海报的网站最近热点新闻事件2023
  • 企业网站管理系统设置网络广告策划书模板范文
  • 网站平台系统设计公司营销策划方案ppt范文
  • 潍坊网站制作发指数是什么
  • 可信验证网站竞价推广托管开户
  • 东莞英文网站制作湖南网站托管
  • 广西网络推广广州seo招聘
  • asp做网站主要技术网站seo优化教程
  • wordpress隐藏文章标题windows优化大师怎么用
  • 中小企业服务中心网站建设百度口碑官网