MongoDB-查询嵌入式文档数组

时间:2020-02-23 14:40:51  来源:igfitidea点击:

在本MongoDB教程中,我们将学习查询一系列嵌入式文档。

登录到您的MongoDB服务器并插入以下文档。

对于本教程,我将把文档插入books集合中。

> db.books.insertMany([
  {
    "title": "The C Programming Language",
    "author": ["Brian Kernighan", "Dennis Ritchie"],
    "inventory": [
      { "warehouse": "W1", "qty": 3000 },
      { "warehouse": "W2", "qty": 750 }
    ]
  },
  {
    "title": "MongoDB in Action",
    "author": ["Kyle Banker"],
    "inventory": [
      { "warehouse": "W2", "qty": 100 },
      { "warehouse": "W3", "qty": 1500 }
    ]
  },
  {
    "title": "Node.js in Action",
    "author": ["Marc Harter", "Nathan Rajlich", "T. J. Holowaychuk", "Mike Cantelon"],
    "inventory": [
      { "warehouse": "W1", "qty": 100 },
      { "warehouse": "W3", "qty": 250 }
    ]
  }
]);

根据阵列中嵌入文档的精确匹配进行选择

在下面的示例中,我们将获取在库存列表数组中具有完全嵌入文档{" warehouse":" W1"," qty":100}的所有文档。

注意!字段顺序很重要,即嵌入文档的第一个字段必须是"仓库"并且必须具有值" W1",并且嵌入文档的第二个字段必须是" qty"并且必须具有值100。

> db.books.find({ "inventory": { "warehouse": "W1", "qty": 100 } }).pretty();

{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c121"),
  "title" : "Node.js in Action",
  "author" : [
    "Marc Harter",
    "Nathan Rajlich",
    "T. J. Holowaychuk",
    "Mike Cantelon"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 100
    },
    {
      "warehouse" : "W3",
      "qty" : 250
    }
  ]
}

如果我们更改查询中嵌入文档的字段顺序,则不会获得结果。

在下面的查询中,我们将嵌入文档的字段顺序更改为{{qty":100," warehouse":" W1"},但没有任何结果。

> db.books.find({ "inventory": { "qty": 100, "warehouse": "W1" } }).pretty();

根据数组中嵌入文档的字段进行选择

在下面的示例中,我们将获取"库存"数组中一个嵌入式文档中"仓库"字段的值为" W1"的所有文档。

> db.books.find({ "inventory.warehouse": "W1" }).pretty();

{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c11f"),
  "title" : "The C Programming Language",
  "author" : [
    "Brian Kernighan",
    "Dennis Ritchie"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 3000
    },
    {
      "warehouse" : "W2",
      "qty" : 750
    }
  ]
}
{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c121"),
  "title" : "Node.js in Action",
  "author" : [
    "Marc Harter",
    "Nathan Rajlich",
    "T. J. Holowaychuk",
    "Mike Cantelon"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 100
    },
    {
      "warehouse" : "W3",
      "qty" : 250
    }
  ]
}

在以下示例中,我们将为"库存"数组中的一个嵌入式文档的" qty"字段获取所有值大于300的文档。

> db.books.find({ "inventory.qty": { $gt: 300 } }).pretty();

{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c11f"),
  "title" : "The C Programming Language",
  "author" : [
    "Brian Kernighan",
    "Dennis Ritchie"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 3000
    },
    {
      "warehouse" : "W2",
      "qty" : 750
    }
  ]
}
{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c120"),
  "title" : "MongoDB in Action",
  "author" : [
    "Kyle Banker"
  ],
  "inventory" : [
    {
      "warehouse" : "W2",
      "qty" : 100
    },
    {
      "warehouse" : "W3",
      "qty" : 1500
    }
  ]
}

根据嵌入式文档字段上的多个查询条件进行选择

在下面的示例中,我们将提取" qty"字段设置为大于100且"仓库"字段设置为" W1"的所有文档。

> db.books.find({ "inventory": { $elemMatch: { "qty": { $gt: 100 }, "warehouse": "W1" } } }).pretty();

{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c11f"),
  "title" : "The C Programming Language",
  "author" : [
    "Brian Kernighan",
    "Dennis Ritchie"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 3000
    },
    {
      "warehouse" : "W2",
      "qty" : 750
    }
  ]
}

在下面的示例中,对于任何一个嵌入式文档,我们将获取所有" qty"字段设置为大于100的文档。

> db.books.find({ "inventory": { $elemMatch: { "qty": { $gt: 100 } } } }).pretty();

{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c11f"),
  "title" : "The C Programming Language",
  "author" : [
    "Brian Kernighan",
    "Dennis Ritchie"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 3000
    },
    {
      "warehouse" : "W2",
      "qty" : 750
    }
  ]
}
{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c120"),
  "title" : "MongoDB in Action",
  "author" : [
    "Kyle Banker"
  ],
  "inventory" : [
    {
      "warehouse" : "W2",
      "qty" : 100
    },
    {
      "warehouse" : "W3",
      "qty" : 1500
    }
  ]
}
{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c121"),
  "title" : "Node.js in Action",
  "author" : [
    "Marc Harter",
    "Nathan Rajlich",
    "T. J. Holowaychuk",
    "Mike Cantelon"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 100
    },
    {
      "warehouse" : "W3",
      "qty" : 250
    }
  ]
}

如果下面的示例中,我们选择" qty"字段设置为大于或者等于200或者" warehouse"字段设置为" W1"的所有文档。

> db.books.find({ "inventory.qty": { $gte: 200 }, "inventory.warehouse": "W1" }).pretty();

{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c11f"),
  "title" : "The C Programming Language",
  "author" : [
    "Brian Kernighan",
    "Dennis Ritchie"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 3000
    },
    {
      "warehouse" : "W2",
      "qty" : 750
    }
  ]
}
{
  "_id" : ObjectId("5d2d9afd81a3cd0a9239c121"),
  "title" : "Node.js in Action",
  "author" : [
    "Marc Harter",
    "Nathan Rajlich",
    "T. J. Holowaychuk",
    "Mike Cantelon"
  ],
  "inventory" : [
    {
      "warehouse" : "W1",
      "qty" : 100
    },
    {
      "warehouse" : "W3",
      "qty" : 250
    }
  ]
}