mongoDB node驱动的增删改查方法

来源:互联网 发布:数据颜色搭配 编辑:程序博客网 时间:2024/06/07 11:45

插入文档

插入单个文档

以下代码将新文档插入到 inventory集合中(如果此集合不存在,则插入操作将创建集合。)

db.collection('inventory').insertOne({  item: "canvas",  qty: 100,  tags: ["cotton"],  size: { h: 28, w: 35.5, uom: "cm" }}).then(function(result) {  // process result})

insertOne() 返回一个promise 对象。

插入多条文档

Collection.insertMany()可以通过一个数组将多个文档 插入到一个集合中。

db.collection('inventory').insertMany([  { item: "journal",    qty: 25,    tags: ["blank", "red"],    size: { h: 14, w: 21, uom: "cm" }},  { item: "mat",    qty: 85,    tags: ["gray"],    size: { h: 27.9, w: 35.5, uom: "cm" }},  { item: "mousepad",    qty: 25,    tags: ["gel", "blue"],    size: { h: 19, w: 22.85, uom: "cm" }}]).then(function(result) {  // process result})

insert()方法可以插入一条或多条文档


查询文档

我们使用MongoDB Node.js驱动程序中的Collection.find()方法进行查询操作。示例使用inventory集合。请先运行以下命令插入一些数据以供查询:

db.collection('inventory').insertMany([  { item: "journal",    qty: 25,    size: { h: 14, w: 21, uom: "cm" },    status: "A"},  { item: "notebook",    qty: 50,    size: { h: 8.5, w: 11, uom: "in" },    status: "A"},  { item: "paper",    qty: 100,    size: { h: 8.5, w: 11, uom: "in" },    status: "D"},  { item: "planner",    qty: 75, size: { h: 22.85, w: 30, uom: "cm" },    status: "D"},  { item: "postcard",    qty: 45,    size: { h: 10, w: 15.25, uom: "cm" },    status: "A"}]).then(function(result) {  // process result})

查询集合中所有文档

要返回集合中的所有文档,将一个空对象作为查询条件传递给find方法即可。

var cursor = db.collection('inventory').find({});

指定查询条件

以下代码从inventory集合查询到所有status 为D的文档:

var cursor = db.collection('inventory').find({ status: "D" });

使用查询运算符

使用查询运算符可以进行一些高级的查询

比如我们想从inventory 集合中检索出所有status等于”A”或”D”的文档:

var cursor = db.collection('inventory').find({   status: { $in: ["A", "D"] }});

要检索出inventory 集合中status等于 “A” 且 qty小于30的所有文档:

var cursor = db.collection('inventory').find({   status: "A",   qty: { $lt: 30 }});

一些条件操作符

名称 描述 $eq 等于 $gt 大于 $gte 大于等于 $in 数组中指定的所有。 $lt 小于 $lte 小于等于 $ne 不等于 $nin 不匹配数组中指定的值

检索出inventory 集合中所有status等于”A” 或 qty小于30的文档:

var cursor = db.collection('inventory').find({   $or: [ {status: "A" }, { qty: { $lt: 30 } } ]});

检索出inventory 集合中status等于”A” 且 qty小于30或item以p开头的文档:

var cursor = db.collection('inventory').find({   status: "A",  $or: [ { qty: { $lt: 30 } }, { item: { $regex: "^p" } } ]});

嵌入/嵌套文档的查询

操作示例前需先插入一些嵌套文档,本示例采用以下文档:

db.collection('inventory').insertMany([  { item: "journal",    qty: 25,    size: { h: 14, w: 21, uom: "cm" },    status: "A"},  { item: "notebook",    qty: 50,    size: { h: 8.5, w: 11, uom: "in" },    status: "A"},  { item: "paper",    qty: 100,    size: { h: 8.5, w: 11, uom: "in" },    status: "D"},  { item: "planner",    qty: 75, size: { h: 22.85, w: 30, uom: "cm" },    status: "D"},  { item: "postcard",    qty: 45,    size: { h: 10, w: 15.25, uom: "cm" },    status: "A"}]).then(function(result) {  // process result})

如果某个字段的值是一个文档(可以理解成js的对象)使用该字段进行查询时 {<field>: <value>} :

例如,以下代码查询字段size等于{ h: 14, w: 21, uom: “cm” }的所有 文档:

var cursor = db.collection('inventory').find({   size: { h: 14, w: 21, uom: "cm" }});

这种方式需要精确匹配包括字段顺序。例如,以下查询与inventory集合中的任何文档都不匹配 :

var cursor = db.collection('inventory').find({   size: { w: 21, h: 14, uom: "cm" }});

要使用嵌入/嵌套文档中的字段作为条件,请使用点符号(”field.nestedField”)。

以下示例查询字段 size中uom字段等于”in”的所有文档:

var cursor = db.collection('inventory').find({   "size.uom": "in"});

使用查询运算符指定匹配

以下代码查询size字段中h字段小于15的所有文档

var cursor = db.collection('inventory').find({   "size.h": { $lt: 15 }});

指定AND条件

以下代码查询size字段中h字段小于15,size字段中uom字段为“in”且status字段为“D”的所有文档

var cursor = db.collection('inventory').find({   "size.h": { $lt: 15 },   "size.uom": "in",   status: "D"});

查询数组

操作示例前需先插入一些字段为数组的文档,本示例采用以下文档:

db.collection('inventory').insertMany([  { item: "journal",    qty: 25,    tags: ["blank", "red"],    dim_cm: [14, 21]},  { item: "notebook",    qty: 50,    tags: ["red", "blank"],    dim_cm: [14, 21]},  { item: "paper",    qty: 100,    tags: ["red", "blank", "plain"],    dim_cm: [14, 21]},  { item: "planner",    qty: 75,    tags: ["blank", "red"],    dim_cm: [22.85, 30]},  { item: "postcard",    qty: 45,    tags: ["blue"],    dim_cm: [10, 15.25]}]).then(function(result) {  // process result})

直接以值为数组的字段作为查询条件,需要精确匹配包括数组顺序:

以下示例查询字段tags 值为具有两个元素的数组,并且按照”red”,”blank”的顺序:

var cursor = db.collection('inventory').find({   tags: [ "red", "blank" ]});

如果要查询数组中含有“red”和“blank”两个元素且不考虑顺序和是否含有其他元素,请使用$all运算符:

var cursor = db.collection('inventory').find({   tags: { $all: [ "red", "blank" ]}});

要查询数组中至少包含一个指定元素,请使用过滤器 { <field>: <value> }

以下示例查询字段tags中包含字符串”red”作为其数组的元素之一:

var cursor = db.collection('inventory').find({   tags: "red"});

如要在数组字段中指定元素的条件,请在 查询过滤器文档({ <array field>: { <operator1>: <value1>, ... } })中使用查询运算符。

以下操作将查询数组dim_cm中至少包含一个值大于25的元素的所有文档 。

var cursor = db.collection('inventory').find({   dim_cm: { $gt: 25 }});

为数组元素指定多个条件

在数组元素上指定多个条件时,需要单个数组元素满足所有条件,或者数组元素的任意组合满足条件。

以下示例查询dim_cm数组包含某些组合可以满足查询条件的元素; 例如,一个元素大于15条件,另一个元素小于20条件,或者单个元素可以满足两者:

var cursor = db.collection('inventory').find({   dim_cm: { $gt: 15, $lt: 20 }});

使用$elemMatch运算符在数组的元素上指定多个条件,使其至少有一个数组元素满足所有指定的条件。

以下示例查询dim_cm数组至少包含一个大于($gt) 22且小($lt)30的元素的所有文档:

var cursor = db.collection('inventory').find({   dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } }});

通过数组索引位置查询

为数组指定索引或位置作为查询条件请使用点(.)符号,索引从零开始。

以下示例查询数组dim_cm中第二个元素大于25的所有文档:

var cursor = db.collection('inventory').find({   "dim_cm.1": { $gt: 25 }});

通过数组的长度查询

使用$size运算符通过数组长度来查询。例如,以下代码检索数组tags具有3个元素的所有文档 。

var cursor = db.collection('inventory').find({   tags: { $size: 3 }});

查询嵌入文档的数组

操作示例前需先插入一些字段为数组且数组元素为文档的文档,本示例采用以下文档:

db.collection('inventory').insertMany([  { item: "journal",    instock: [      { warehouse: "A", qty: 5 },      { warehouse: "C", qty: 15 }]},  { item: "notebook",    instock: [      { warehouse: "C", qty: 5 }]},  { item: "paper",    instock: [      { warehouse: "A", qty: 60 },      { warehouse: "B", qty: 15 }]},  { item: "planner",    instock: [      { warehouse: "A", qty: 40 },      { warehouse: "B", qty: 5 }]},  { item: "postcard",    instock: [      { warehouse: "B", qty: 15 },      { warehouse: "C", qty: 35 }]}]).then(function(result) {  // process result})

查询嵌套在数组中的文档

以下示例检索instock数组中的元素与指定文档匹配的所有文档:

var cursor = db.collection('inventory').find({   instock: { warehouse: "A", qty: 5 }});

此匹配需要精确匹配,包括字段顺序。例如,以下查询与inventory集合中的任何文档不匹配 :

var cursor = db.collection('inventory').find({   instock: { qty: 5, warehouse: "A" }});

使用数组索引对应的文档的字段查询:

你可以使用点符号在文档中的数组的特定索引或位置处指定嵌入文档的字段作为查询条件。

以下示例检索instock数组的第一个元素的qty字段的值小于或等于20的所有文档:

var cursor = db.collection('inventory').find({   "instock.0.qty": { $lte: 20 }});

如果不知道嵌套在数组中的文档的索引位置,则将数组字段的名称与点(.)和嵌套文档中的字段的名称相连接。

以下示例检索instock数组中至少包含一个对象其qty字段的值小于或等于20的所有文档:

var cursor = db.collection('inventory').find({   "instock.qty": { $lte: 20 }});

为数组指定多个条件

单个嵌套文档满足多个查询条件
使用$elemMatch运算符在嵌入式文档数组上指定多个条件是,需至少一个嵌入文档满足所有指定的条件

以下示例查询到的文档,其中instock数组至少包含一个对象其qty字段等于5且warehouse字段等于A:

var cursor = db.collection('inventory').find({   instock: { $elemMatch: { qty: 5, warehouse: "A" } }});

以下示例查询instock数组至少包含一个对象其qty字段大于10且小于或等于20:

var cursor = db.collection('inventory').find({   instock: { $elemMatch: { qty: { $gt: 10, $lte: 20 } } }});

如果数组字段上的复合查询条件不使用 $elemMatch运算符,则将查询到其数组包含满足条件的任意元素组合的所有文档。。

例如,以下查询匹配到的文档,其中嵌套在instock数组中的任何文档的qty字段大于10且任何文档(但不一定相同的嵌入文档)的qty字段小于或等于20:

var cursor = db.collection('inventory').find({   "instock.qty": { $gt: 10, $lte: 20 }});

以下示例查询到的文档,其中instock数组至少包含一个qty字段等于5和一个warehouse字段等于A的对象:

var cursor = db.collection('inventory').find({   "instock.qty": 5, "instock.warehouse": "A"});

选择返回哪些字段

默认情况下,MongoDB中的查询返回匹配文档中的所有字段。为了限制MongoDB发送给应用程序的数据量,您可以包括一个映射文档来指定或限制要返回的字段。

操作示例前需先插入一些文档,本示例采用以下文档:

db.collection('inventory').insertMany([  { item: "journal",    status: "A",    size: { h: 14, w: 21, uom: "cm" },    instock: [ { warehouse: "A", qty: 5 } ]},  { item: "notebook",    status: "A",    size: { h: 8.5, w: 11, uom: "in" },    instock: [ { warehouse: "C", qty: 5 } ]},  { item: "paper",    status: "D",    size: { h: 8.5, w: 11, uom: "in" },    instock: [ { warehouse: "A", qty: 60 } ]},  { item: "planner",    status: "D",    size: { h: 22.85, w: 30, uom: "cm"},    instock: [ { warehouse: "A", qty: 40 } ]},  { item: "postcard",    status: "A",    size: { h: 10, w: 15.25, uom: "cm" },    instock: [        { warehouse: "B", qty: 15 },        { warehouse: "C", qty: 35 }]}]).then(function(result) {  // process result})

返回匹配文档中的所有字段

如果不指定映射文档,则 find()方法将返回匹配文档中的所有字段。

以下示例返回inventory集合中所有status等于”A”的所有文档的所有字段

var cursor = db.collection('inventory').find({   status: "A"});

只返回指定字段和_id字段

可以通过在映射文档中设置要显示的字段为1来声明要返回的字段。以下操作返回与查询匹配的所有文档只包含item和status字段。对于该_id字段,您不必明确指定返回该字段。除非指定要禁止该字段,否则该方法始终返回_id字段

var cursor = db.collection('inventory').find({   status: "A"}).project({ item: 1, status: 1 });

禁止_id字段

可以在映射文档中设置_id字段等于零来禁止其返回,如下面的例子:

var cursor = db.collection('inventory').find({   status: "A"}).project({ item: 1, status: 1, _id: 0 });

设置排除的字段

您可以使用映射来排除特定字段,而不是列出要在匹配文档中返回的字段。以下示例返回匹配文档中除了status和instock字段之外的所有字段:

var cursor = db.collection('inventory').find({   status: "A"}).project({ status: 0, instock: 0 });

除了_id字段外,不能在投影文档中组合包含和排除语句。

返回特定字段内嵌文件

可以在嵌入式文档中返回特定字段。使用点表示法来引用嵌入字段,并将其在映射文件中设置为1。

以下示例返回: 文档中的_id字段(默认返回), item字段,status字段和size中的uom字段; uom字段仍然嵌入在size文档中。

var cursor = db.collection('inventory').find({   status: "A"}).project({ item: 1, status: 1, "size.uom": 1 });

排除内嵌文件中具体字段

可以禁止嵌入文档中的特定字段。使用 点符号来引用映射文档中的嵌入字段并设置为0。

以下示例指定要排除文档中size字段的uom 字段。所有其他字段在匹配的文档中返回:

var cursor = db.collection('inventory').find({   status: "A"}).project({ "size.uom": 0 });

嵌入式文档在数组中的映射

使用点表示法来嵌入数组中的文档内的特定字段。

以下示例指定一个映射来返回文档中的item字段,status字段和嵌入到instock数组中文档的qty字段。_id默认返回。

var cursor = db.collection('inventory').find({   status: "A"}).project({ item: 1, status: 1, "instock.qty": 1 });

返回数组中的特定元素

对于包含数组的字段,MongoDB的提供了如下的投映射运算符:$elemMatch$slice,和$

以下示例使用$slice 映射运算符只返回instock数组中的最后一个元素。

var cursor = db.collection('inventory').find({   status: "A"}).project({ item: 1, status: 1, "instock": { $slice: -1 } });

$elemMatch$slice$是将特定元素映射到包含在返回的数组中的唯一方法。例如,您不能 使用数组索引来映射特定的数组元素; 例如此映射不会使用第一个元素映射数组。{ “instock.0”: 1 }

原创粉丝点击