MongoDB插入命令
它在集合中插入一个或多个文档,并返回包含所有输入状态的文档。 INSERT方法在内部使用INSERT命令,由MongoDB提供。
语法:
{
insert: <collection>,
documents: [ <document>, <document>, <document>, ... ],
ordered: <boolean>,
writeConcern: { <write concern> },
bypassDocumentValidation: <boolean>
}
参数字段
| 字段 | 类型 | 描述 |
|---|---|---|
| insert | string | 插入元素的集合 |
| documents | array | 它是想要插入集合的文档数组 |
| ordered | boolean | 设置为true则返回结果 |
| writeConcern | document | 定义了插入命令写入 |
| bypass Document Validation | boolean | 它使写入操作能够绕过文档验证。 |
示例:
让无涯教程将文档插入书籍集合:
db.runCommand(
{
insert: "books",
documents: [ { _id: 1, bookname: "MongoDB", status: "sold" } ]
}
)
mongodb删除命令
无涯教程可以使用DELETE命令从集合中删除任何文档。
语法:
{
delete: <collection>,
deletes: [
{ q : <query>, limit : <integer>, collation: <document> },
{ q : <query>, limit : <integer>, collation: <document> },
{ q : <query>, limit : <integer>, collation: <document> },
...
],
ordered: <boolean>,
writeConcern: { <write concern> }
}
参数字段
| Field | Type | Description |
|---|---|---|
| delete | string | 删除元素的目标集合名称。 |
| deletes | array | 执行删除操作的删除语句数组。 |
| ordered | boolean | 如果它设置为true则返回结果。 |
| writeConcern | document | 定义了删除命令写入。 |
| q | document | 它是匹配删除的查询。 |
| limit | integer | 无涯教程可以使用此字段限制匹配文档来删除。指定0以删除所有匹配的文档。 |
| collation | document | 它是一个可选字段,并用于定义用于操作的排序规则。 Syntax: collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
|
例子:
以下示例通过指定限制2,从书籍集合中删除了与status等于A的文档。
db.runCommand(
{
delete: "books",
deletes: [ { q: { status: "A" }, limit: 1 } ]
}
)
MongoDB更新命令
更新命令在集合中更改文档。它包含多个更新语句。它由MongoDB驱动程序提供的更新方法使用。
语法:
db.runCommand(
{
update: <collection>,
updates: [
{
q: <query>,
u: <document or pipeline>, //Changed in MongoDB 4.2,
upsert: <boolean>,
multi: <boolean>,
collation: <document>,
arrayFilters: <array>,
hint: <document|string> //Available starting in MongoDB 4.2
},
...
],
ordered: <boolean>,
writeConcern: { <write concern> },
bypassDocumentValidation: <boolean>
}
)
命令字段:
| Field | Type | Description |
|---|---|---|
| update | string | 是无涯教程想要更新数组的目标集合名称。 |
| updates | array | 它是更新语句的数组,以在给定集合上执行更新操作。 |
| ordered | boolean | 如果它设置为true,将返回结果而不执行剩余的更新操作。 |
| writeConcern | document | 它是一种表示更新命令的写入问题的文档。 |
| bypass Document Validation |
boolean | 它使更新操作能够绕过文档验证。 |
| q | document | 它是与无涯教程要更新的文档匹配的查询。 |
| u | document | 它是存储更新运算符表达式的文档。 |
| upsert | boolean | 如果此字段设置为true,则如果没有文档与查询匹配,则执行插入操作。 |
| multi | boolean | 它这个字段设置为true;它将更新符合查询条件的所有文档。 |
| collation | document | 它指定了字符串比较的语言特定规则。 Syntax: collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
|
| arrayfilters | array | 它是一系列文档,用于描述无涯教程要修改的数组元素。 |
| hint | string/ document |
它是一个文档,用于指定用于支持查询的索引。 |
示例:
让无涯教程创建一个学生的集合
Db.students.insertMany([
{ _id: 1, student: "john", status: "Pending", points: 0, misc1: "note to self: confirm status", misc2: "Need to activate" },
{ _id: 2, student: "Michael", status: "D", points: 59, misc1: "reminder: ping me at 100pts", misc2: "Some random comment" },
])
RUN命令使用$SET和$INC运营商更新student等于"JOHN"的文档的状态。
db.runCommand(
{
update: "students",
updates: [
{
q: { student: "john" }, u: { $set: { status: "A" }, $inc: { points: 1 } }
}
],
ordered: false,
writeConcern: { w: "majority", wtimeout: 5000 }
}
)
MongoDB找到命令
Find命令用于执行查询并返回第一组结果以及无涯教程可以构建游标的光标的ID。
语法:
db.runCommand({ "find": <string>, "filter": <document>, "sort": <document>, "projection": <document>, "hint": <document or string>, "skip": <int>, "limit": <int>, "batchSize": <int>, "singleBatch": <bool>, "comment": <string>, "maxTimeMS": <int>, "readConcern": <document>, "max": <document>, "min": <document>, "returnKey": <bool>, "showRecordId": <bool>, "tailable": <bool>, "oplogReplay": <bool>, "noCursorTimeout": <bool>, "awaitData": <bool>, "allowPartialResults": <bool>, "collation": <document> } )
命令字段:
| Field | Type | Description |
|---|---|---|
| find | string | 在此字段中,无涯教程可以定义集合的名称。 |
| filter | document | 它过滤查询。 |
| sort | document | 它是一个包含查询的排序详细信息的文档。 |
| projection | document | 它是包含规范的文档,以确定要在返回的文档中包含的字段。 |
| hint | string | 它是一个文档,它将索引名称指定为字符串或索引密钥模式。 |
| skip | positive integer | 此文件包含要跳过的文件数。 |
| limit | Non-negative integer | 无涯教程可以设置要返回的最大文档数。 |
| batchSize | Non-negative integer | 它包含无涯教程要在第一个批处理中返回的文档数量。 |
| singleBatch | boolean | 它包含在第一批结果后是否关闭光标。 |
| maxTimeMS | +ve integer | 无涯教程可以设置光标上的处理操作的时间限制。 |
| readConcern | document | 它指定了读取的级别。ReadConcern: { level: <value> }
|
| max | document | 它包含给定索引的上限。 |
| min | boolean | 它包含给定索引的下限。 |
| returnKey | boolean | 如果是true,则返回生成文档中的索引键。 |
| showRecordID | boolean | 它用于返回每个文档的记录标识符。 |
| tailable | boolean | 它返回一个可定制的光标,用于加盖收集。 |
| awaitData | boolean | 它用于暂时阻止游标上的getMore命令。 |
| oplogReplay | boolean | 它是用于重播副本集的Oplog的命令。例如 -{ find: "data", oplogReplay: true, filter:
{ ts: { $gte: new Timestamp(1514764800, 0) } } }
|
| noCursorTimeout | boolean | 此文件以防止服务器超时闲置游标。 |
| allowPartialResults | boolean | 如果某些碎片不可用,则此字段可防止抛出错误。 |
| collation | document | 它指定了操作的排序规则 Syntax: collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
|
示例:
在下面的示例中,该命令在名称字段中设置的结果中的文档排序,并限制六个文档集的结果。
db.runCommand(
{
find: "restaurants",
filter: { rating: { $gte: 9 }, cuisine: "American" },
projection: { name: 1, rating: 1, address: 1 },
sort: { name: 1 },
limit: 6
}
)
mongodb findandmodify命令
它一次修改并返回单个文档。返回的文档不包括默认情况下在更新上进行的修改。无涯教程需要使用新选项来返回修改后的文档。
语法:
{
findAndModify: <collection-name>,
query: <document>,
sort: <document>,
remove: <boolean>,
update: <document or aggregation pipeline>,//Changed in MongoDB 4.2
new: <boolean>,
fields: <document>,
upsert: <boolean>,
bypassDocumentValidation: <boolean>,
writeConcern: <document>,
collation: <document>,
arrayFilters: <array>
}
命令字段
| Field | Type | Description |
|---|---|---|
| query | document | 查询字段包含与db.collection.find()方法中使用的相同的查询选择器。 |
| sort | document | 它定义了文档的排序顺序。 |
| remove | boolean | 此字段删除查询字段中指定的文档。 |
| update | document/ array | 它将更新指定的文档。 |
| new | boolean | 如果设置为true,它将返回修改后的文档而不是原始文件。 |
| fields | document | 它是要返回的字段的子集。它指定包含值1的字段。fields: { <field1>: 1, <field2>: 1, ... }
|
| upsert | boolean | 它与更新的字段一起使用。如果是真,它会创建一个新文档并更新与查询匹配的单个文档。此提交的默认值为false。 |
| bypass Document Validation | boolean | 它可以在此过程中找到findandmodify来绕过文档验证。 |
| writeConcern | document | 它是一种表达命令的写信的文档。 |
| maxTimeMS | integer | 它声明了操作的时间限制。 |
| FindAndModify | String | 此字段包含无涯教程必须运行命令的集合。 |
| collation | Collation字段允许用户为字符串比较指定特定于语言的规则。句法:collation: {
locale: <string>,
caseLevel: <boolean>,
caseFirst: <string>,
strength: <int>,
numericOrdering: <boolean>,
alternate: <string>,
maxVariable: <string>,
backwards: <boolean>
}
|
|
| arrayFilters | array | 它是一个过滤器文档的数组,可以确定定义哪些数组元素被修改以进行更新操作。 |
示例:
db.runCommand(
{
findAndModify: "book",
query: { name: "MongoDB" },
sort: { rating: 4 },
update: { $inc: { price: 1 } },
upsert: true
}
)