MongoDB Query Array

[
  { item: "journal", qty: 25, tags: ["blank", "red"], dim_cm: [ 14, 21 ] },  # 1
  { item: "notebook", qty: 50, tags: ["red", "blank"], dim_cm: [ 14, 21 ] },  # 2
  { item: "paper", qty: 100, tags: ["red", "blank", "plain"], dim_cm: [ 14, 21 ] },  # 3
  { item: "planner", qty: 75, tags: ["blank", "red"], dim_cm: [ 22.85, 30 ] },  # 4
  { item: "postcard", qty: 45, tags: ["blue"], dim_cm: [ 10, 15.25 ] }  # 5
]

1.查询tags对应序列为["red", "blank"]的doc,序列元素内容和顺序必须一致:
{ tags: ["red", "blank"] } -> 2
2.查询tags对应序列为["red", "blank"]的doc,序列元素顺序不需要一致:
{ tags: { $all: ["red", "blank"] } } ->1,2,3,4
3.查询tags对应序列包含"red"的doc
{ tags: "red" } -> 1,2,3,4
4.查询dim_cm对应序列中包含大于25元素的doc
{ dim_cm: { $gt: 25 } } ->4
5.查询dim_cm对应序列中包含大于15元素小于20的doc
{ dim_cm: { $gt: 15, $lt: 20 } } -> 1,2,3,5
6.查询dim_cm对应序列中包含大于22元素小于30的doc
{ dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } ->4
7.查询dim_cm对应序列中第一个元素大于25的doc
{ "dim_cm.1": { $gt: 25 } } -->4
8.查询tags对应序列为3的doc
{ "tags": { $size: 3 } } # 删选tags,长度为3 的doc

参考:
https://docs.mongodb.com/manual/tutorial/query-arrays/

你可能感兴趣的:(mongodb,mongodb)