首页 > 其他分享 >$elemMatch (query)

$elemMatch (query)

时间:2023-08-12 09:11:22浏览次数:44  
标签:product xyz results id score query elemMatch

 

https://www.mongodb.com/docs/manual/reference/operator/query/elemMatch/

$elemMatch (query)

 

TIP

See also:

$elemMatch (projection)

 
$elemMatch
 

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
 

If you specify only a single <query> condition in the $elemMatch expression, and are not using the $not or $ne operators inside of $elemMatch$elemMatch can be omitted. See Single Query Condition.

     

Given the following documents in the scores collection:

{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }
 

The following query matches only those documents where the results array contains at least one element that is both greater than or equal to 80 and is less than 85:

db.scores.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)
 

The query returns the following document since the element 82 is both greater than or equal to 80 and is less than 85:

{ "_id" : 1, "results" : [ 82, 85, 88 ] }
 

For more information on specifying multiple criteria on array elements, see Specify Multiple Conditions for Array Elements.

 

This statement inserts documents into the survey collection:

db.survey.insertMany( [
{ "_id": 1, "results": [ { "product": "abc", "score": 10 },
{ "product": "xyz", "score": 5 } ] },
{ "_id": 2, "results": [ { "product": "abc", "score": 8 },
{ "product": "xyz", "score": 7 } ] },
{ "_id": 3, "results": [ { "product": "abc", "score": 7 },
{ "product": "xyz", "score": 8 } ] },
{ "_id": 4, "results": [ { "product": "abc", "score": 7 },
{ "product": "def", "score": 8 } ] }
] )
 

The following query matches only those documents where the results array contains at least one element with both product equal to "xyz" and score greater than or equal to 8:

db.survey.find(
{ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)
 

Specifically, the query matches the following document:

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }
 

If you specify a single query predicate in the $elemMatch expression, and are not using the $not or $ne operators inside of $elemMatch$elemMatch can be omitted.

The following examples return the same documents.

With $elemMatch:

db.survey.find(
{ results: { $elemMatch: { product: "xyz" } } }
)
 

Without $elemMatch:

db.survey.find(
{ "results.product": "xyz" }
)
 

However, if your $elemMatch expression contains the $not or $ne operators then omitting the $elemMatch expression changes the documents returned.

The following examples return different documents.

With $elemMatch:

db.survey.find(
{ "results": { $elemMatch: { product: { $ne: "xyz" } } } }
)
 

Without $elemMatch:

db.survey.find(
{ "results.product": { $ne: "xyz" } }
)
 

With $elemMatch, the first query returns these documents:

{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 },
{ "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 },
{ "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

Without $elemMatch, the second query returns this document:

{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

$elemMatch (query)

 

TIP

See also:

$elemMatch (projection)

 
$elemMatch
 

The $elemMatch operator matches documents that contain an array field with at least one element that matches all the specified query criteria.

{ <field>: { $elemMatch: { <query1>, <query2>, ... } } }
 

If you specify only a single <query> condition in the $elemMatch expression, and are not using the $not or $ne operators inside of $elemMatch$elemMatch can be omitted. See Single Query Condition.

     

Given the following documents in the scores collection:

{ _id: 1, results: [ 82, 85, 88 ] }
{ _id: 2, results: [ 75, 88, 89 ] }
 

The following query matches only those documents where the results array contains at least one element that is both greater than or equal to 80 and is less than 85:

db.scores.find(
{ results: { $elemMatch: { $gte: 80, $lt: 85 } } }
)
 

The query returns the following document since the element 82 is both greater than or equal to 80 and is less than 85:

{ "_id" : 1, "results" : [ 82, 85, 88 ] }
 

For more information on specifying multiple criteria on array elements, see Specify Multiple Conditions for Array Elements.

 

This statement inserts documents into the survey collection:

db.survey.insertMany( [
{ "_id": 1, "results": [ { "product": "abc", "score": 10 },
{ "product": "xyz", "score": 5 } ] },
{ "_id": 2, "results": [ { "product": "abc", "score": 8 },
{ "product": "xyz", "score": 7 } ] },
{ "_id": 3, "results": [ { "product": "abc", "score": 7 },
{ "product": "xyz", "score": 8 } ] },
{ "_id": 4, "results": [ { "product": "abc", "score": 7 },
{ "product": "def", "score": 8 } ] }
] )
 

The following query matches only those documents where the results array contains at least one element with both product equal to "xyz" and score greater than or equal to 8:

db.survey.find(
{ results: { $elemMatch: { product: "xyz", score: { $gte: 8 } } } }
)
 

Specifically, the query matches the following document:

{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }
 

If you specify a single query predicate in the $elemMatch expression, and are not using the $not or $ne operators inside of $elemMatch$elemMatch can be omitted.

The following examples return the same documents.

With $elemMatch:

db.survey.find(
{ results: { $elemMatch: { product: "xyz" } } }
)
 

Without $elemMatch:

db.survey.find(
{ "results.product": "xyz" }
)
 

However, if your $elemMatch expression contains the $not or $ne operators then omitting the $elemMatch expression changes the documents returned.

The following examples return different documents.

With $elemMatch:

db.survey.find(
{ "results": { $elemMatch: { product: { $ne: "xyz" } } } }
)
 

Without $elemMatch:

db.survey.find(
{ "results.product": { $ne: "xyz" } }
)
 

With $elemMatch, the first query returns these documents:

{ "_id" : 1, "results" : [ { "product" : "abc", "score" : 10 },
{ "product" : "xyz", "score" : 5 } ] }
{ "_id" : 2, "results" : [ { "product" : "abc", "score" : 8 },
{ "product" : "xyz", "score" : 7 } ] }
{ "_id" : 3, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "xyz", "score" : 8 } ] }
{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

Without $elemMatch, the second query returns this document:

{ "_id" : 4, "results" : [ { "product" : "abc", "score" : 7 },
{ "product" : "def", "score" : 8 } ] }

标签:product,xyz,results,id,score,query,elemMatch
From: https://www.cnblogs.com/kungfupanda/p/17624349.html

相关文章

  • jquery 特效专辑
     提示框弹窗类FaceboxFacebox是一个基于jQuery,Facebook-style的lightbox。能够展示示images,divs或者整个远程页面。FaceboxSimpleModalSimpleModal是一个轻量级jQuery插件提供了一个简单的接口来创建模式对话框。SimpleModaljTipjTip一个利用jQuery开发的提示......
  • QueryPath, php上的jQuery
      红得发紫的jQuery框架是专门用于页面Javascript程序设计的,它通过一种优雅的方式让我们轻松自如地操作页面的所有元素而无须担心浏览器版本以及兼容性等问题。受到jQuery的启发,一种试图让Web开发者在PHP中直接采用jQuery方式操纵和生成HTML/XML元素的QueryPath计划开始了,库的......
  • jQuery 操作select
    jQuery取得select选中的值   本来以为jQuery("#select1").val();是取得选中的值,       那么jQuery("#select1").text();就是取得的文本。       这是不正确的,正确做法是:       jQuery("#select1 option:selected").text();jQuery取得select选择......
  • 谷歌云 | BigQuery 现在支持用于查询开放表格式的清单文件
    【本文由CloudAce整理发布。CloudAce是谷歌云全球战略合作伙伴,拥有300多名工程师,也是谷歌最高级别合作伙伴,多次获得GoogleCloud合作伙伴奖。作为谷歌托管服务商,我们提供谷歌云、谷歌地图、谷歌办公套件、谷歌云认证培训服务。】开放表格式依赖嵌入式元数据来提供事务一致的......
  • MySQL全文搜索的高级特性:查询扩展(Query Expansion)
    查询扩展(QueryExpansion)是全文搜索的一个高级特性,尤其对于某些搜索需求来说非常有用。它是基于原始查询返回的结果来进一步扩展并改进搜索结果的过程。当用户执行全文搜索查询时,可能会遇到以下情况:查询结果太少或没有。由于用户不熟悉正确的术语或关键字,查询不准确。在这些......
  • SuiteQL Query Tool(from Tim)
    背景使用了3年时间后,我表示非常感激;不得不来赞美一下下。我很喜欢Tim兄分享的SuiteQLQueryTool,它用AJAX的方式提交query无需刷新页面动态加载query结果,另外更加人性化的数据库字段搜索,索引与关联等。总体感觉:非常简洁,直观,方便,快速源地址https://timdietrich.me/netsuite......
  • jQuery隐式迭代
      ......
  • DOM对象和jquery对象互换
      ......
  • DOM对象和jquery对象
      ......
  • jquery基本使用
          ......