MongoDB - 查询和写入操作命令

MongoDB - 查询和写入操作命令 首页 / MongoDB入门教程 / MongoDB - 查询和写入操作命令

MongoDB插入命令

它在集合中插入一个或多个文档,并返回包含所有输入状态的文档。 INSERT方法在内部使用INSERT命令,由MongoDB提供。

语法:

{
   insert: <collection>,
   documents: [ <document>, <document>, <document>, ... ],
   ordered: <boolean>,
   writeConcern: { <write concern> },
   bypassDocumentValidation: <boolean>
}

参数字段

字段类型描述
insertstring插入元素的集合
documentsarray它是想要插入集合的文档数组
ordered boolean设置为true则返回结果
writeConcerndocument定义了插入命令写入
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> }
}

参数字段

FieldTypeDescription
deletestring删除元素的目标集合名称。
deletesarray执行删除操作的删除语句数组。
orderedboolean如果它设置为true则返回结果。
writeConcerndocument定义了删除命令写入。
qdocument它是匹配删除的查询。
limitinteger无涯教程可以使用此字段限制匹配文档来删除。指定0以删除所有匹配的文档。
collationdocument它是一个可选字段,并用于定义用于操作的排序规则。
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>
   }
)

命令字段:

FieldTypeDescription
updatestring是无涯教程想要更新数组的目标集合名称。
updatesarray它是更新语句的数组,以在给定集合上执行更新操作。
orderedboolean如果它设置为true,将返回结果而不执行剩余的更新操作。
writeConcerndocument它是一种表示更新命令的写入问题的文档。
bypass Document Validation
boolean 它使更新操作能够绕过文档验证。
qdocument它是与无涯教程要更新的文档匹配的查询。
udocument它是存储更新运算符表达式的文档。
upsertboolean如果此字段设置为true,则如果没有文档与查询匹配,则执行插入操作。
multiboolean它这个字段设置为true;它将更新符合查询条件的所有文档。
collationdocument它指定了字符串比较的语言特定规则。
Syntax:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
arrayfiltersarray它是一系列文档,用于描述无涯教程要修改的数组元素。
hintstring/
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>
   }
)

命令字段:

FieldTypeDescription
findstring在此字段中,无涯教程可以定义集合的名称。
filterdocument它过滤查询。
sortdocument它是一个包含查询的排序详细信息的文档。
projectiondocument它是包含规范的文档,以确定要在返回的文档中包含的字段。
hintstring它是一个文档,它将索引名称指定为字符串或索引密钥模式。
skippositive integer此文件包含要跳过的文件数。
limitNon-negative integer无涯教程可以设置要返回的最大文档数。
batchSizeNon-negative integer它包含无涯教程要在第一个批处理中返回的文档数量。
singleBatchboolean它包含在第一批结果后是否关闭光标。
maxTimeMS+ve integer无涯教程可以设置光标上的处理操作的时间限制。
readConcerndocument它指定了读取的级别。
ReadConcern: { level: <value> }
maxdocument它包含给定索引的上限。
minboolean它包含给定索引的下限。
returnKeyboolean如果是true,则返回生成文档中的索引键。
showRecordIDboolean它用于返回每个文档的记录标识符。
tailableboolean它返回一个可定制的光标,用于加盖收集。
awaitDataboolean它用于暂时阻止游标上的getMore命令。
oplogReplayboolean它是用于重播副本集的Oplog的命令。例如 -
{ find: "data", oplogReplay: true, filter: 
		{ ts: { $gte: new Timestamp(1514764800, 0) } } }
noCursorTimeoutboolean此文件以防止服务器超时闲置游标。
allowPartialResultsboolean如果某些碎片不可用,则此字段可防止抛出错误。
collationdocument它指定了操作的排序规则
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>
}

命令字段

FieldTypeDescription
querydocument查询字段包含与db.collection.find()方法中使用的相同的查询选择器。
sortdocument它定义了文档的排序顺序。
removeboolean此字段删除查询字段中指定的文档。
updatedocument/ array它将更新指定的文档。
newboolean如果设置为true,它将返回修改后的文档而不是原始文件。
fieldsdocument它是要返回的字段的子集。它指定包含值1的字段。
fields: { <field1>: 1, <field2>: 1, ... }
upsertboolean它与更新的字段一起使用。如果是真,它会创建一个新文档并更新与查询匹配的单个文档。此提交的默认值为false。
bypass Document Validationboolean它可以在此过程中找到findandmodify来绕过文档验证。
writeConcerndocument它是一种表达命令的写信的文档。
maxTimeMSinteger它声明了操作的时间限制。
FindAndModifyString此字段包含无涯教程必须运行命令的集合。
collation Collat​​ion字段允许用户为字符串比较指定特定于语言的规则。句法:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
}
arrayFiltersarray它是一个过滤器文档的数组,可以确定定义哪些数组元素被修改以进行更新操作。

示例:

db.runCommand(
   {
     findAndModify: "book",
     query: { name: "MongoDB" },
     sort: { rating: 4 },
     update: { $inc: { price: 1 } },
     upsert: true
   }
 )

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

程序员进阶攻略 -〔胡峰〕

TensorFlow快速入门与实战 -〔彭靖田〕

玩转webpack -〔程柳锋〕

MongoDB高手课 -〔唐建法(TJ)〕

分布式数据库30讲 -〔王磊〕

Flink核心技术与实战 -〔张利兵〕

成为AI产品经理 -〔刘海丰〕

爆款文案修炼手册 -〔乐剑峰〕

说透5G -〔杨四昌〕

好记忆不如烂笔头。留下您的足迹吧 :)