MongoDB教程
MongoDB运算符
MongoDB命令
MongoDB数据库
MongoDB Shell
MongoDB云和工具

Mongodb 查询和写入

MongoDB插入命令

它将在集合中插入一个或多个文档,还返回包含以下内容的文档所有输入的状态。 insert方法在内部使用由MongoDB提供的insert命令。
语法:
{
   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命令从集合中删除任何文档。一个删除命令中有多个删除规范。我们不能在上限集合中使用它。删除命令在内部由MongoDB提供的删除方法使用。
语法:
{
   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> }
}
参数字段
字段 类型 说明
delete string 这是我们要在其中删除元素的目标集合的名称。
delete array 这是我们要在其上执行删除操作的删除语句的数组。
ordered boolean 如果将其设置为true,则在插入操作失败时返回结果,而不插入插入数组中列出的任何剩余文档,反之亦然
writeConcern document 这是一个定义delete命令的写入关注点的文档。
q document 是要删除的匹配查询。
limit integer 我们可以使用此字段限制要删除的匹配文档。指定0以删除所有匹配的文档,反之亦然。
collation document 这是一个可选字段,用于定义用于操作的排序规则。
语法:
 collat​​ion: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable: <string>,
   backwards: <boolean>
} 
示例:
下面的示例通过指定限制2从状态为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>
   }
)
命令字段:
字段 类型 说明
update string 这是我们要在其中更新数组的目标集合的名称。
update array 这是更新语句的数组,用于对给定的集合执行更新操作。
ordered boolean 如果设置为true,则为可选字段。它将不执行剩余的更新操作就返回结果,反之亦然。
writeConcern document 这是一个文档,表达了update命令的写入问题。它描述了MongoDB请求对独立MongoDB进行写操作的确认级别。
bypass
文档
验证
boolean它使更新操作可以绕过文档验证。
q document 这是与我们要更新的文档匹配的查询。
u document 这是存储更新运算符表达式的文档。
upsert boolean 如果此字段设置为true,那么如果没有文档与查询匹配,它将执行插入操作。
multi boolean 此字段设置为true;它将更新所有符合查询条件的文档。
collation document 它指定用于字符串比较的特定于语言的规则。
语法:
collation: {
   locale: <string>,
   caseLevel: <boolean>,
   caseFirst: <string>,
   strength: <int>,
   numericOrdering: <boolean>,
   alternate: <string>,
   maxVariable:<string>,
   backwards: <boolean>
} 
arrayfilters array 这是一个文档数组,用于描述我们要修改的数组元素。
hint string/
document
这是一个文档,用于指定用于支持查询谓词的索引。
示例:
让我们创建students收藏集
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运算符更新成员等于" john"的文档的状态。
db.runCommand(
   {
      update: "students",
      updates: [
         {
           q: { student: "john" }, u: { $set: { status: "A" }, $inc: { points: 1 } }
         }
      ],
      ordered: false,
      writeConcern: { w: "majority", wtimeout: 5000 }
   }
)

MongoDB查找命令

查找命令用于执行查询,并返回第一组结果和可以用来构造游标的游标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>
   }
)
命令字段:
字段 类型 说明
find string 在此字段中,我们可以定义集合的名称。
filter document 它过滤查询。
sort document 这是一个包含查询排序详细信息的文档。
projection document 它是包含投影说明的文档,用于确定要包含在返回的文档中的字段。
hint string 这是一个将索引名称指定为字符串或索引键模式的文档。
skip 正整数 此字段包含要跳过的文档数。
limit 非负整数 我们可以设置要返回的最大文档数。
batchSize 非负整数 它包含我们要在第一批中返回的文档数。
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 这是用于重放副本集操作日志的命令。例如-
 {find: " data",oplogReplay: true,filter: 
{ts: {$gte: new Timestamp(1514764800,0)}}} 
noCursorTimeout boolean 此文件是为了防止服务器使空闲游标超时。
allowPartialResults boolean 如果某些分片不可用,此字段可防止引发错误。
collation document 它指定操作的归类。
语法:
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>
}
命令字段
字段 类型 说明
query document 查询字段包含与db.collection.find()方法相同的查询选择器。
sort document 它定义了文档的排序顺序。
remove 布尔值 此字段将删除查询字段中指定的文档。
update 文档/数组 它将更新指定的文档。
new 布尔值 如果设置为true,它将返回修改后的文档,而不是原始文档。
fields document 这是要返回的字段的子集。它指定包含值1的字段。
 fields: {
       
        : 1,1,
        
         : 1,...} 
        
       
upsert 布尔值 它与更新的字段一起使用。如果为true,它将创建一个新文档并更新与查询匹配的单个文档。此字段的默认值为false。
bypass
文档
验证
布尔值 它使findAndModify可以在此过程中绕过文档验证。
writeConcern document 这是一个表达命令关注点的文档。
maxTimeMS 整数 它声明了操作的时间限制。
FindAndModify 字符串 此字段包含我们必须针对其运行命令的集合。
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
   }
 )

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4