23

MongoDB CRUD操作中的R

 4 years ago
source link: http://mp.weixin.qq.com/s?__biz=MzAwOTU4NzM5Ng%3D%3D&%3Bmid=2455771513&%3Bidx=1&%3Bsn=fddbf82a46a1ff1704b1b70383d4d4e1
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

mongdb crud操作比mysql的crud操作相对复杂一点,主要原因在于mongodb中包含数组和字典,本文记录下,方便自己温习。

几个概念

SQL to MongoDB Mapping Chart(docs.mongodb.com/manual/reference/sql-comparison) 很有用,大部分人对SQL很熟悉,这个文档可以帮助你以SQL的方式理解mongodb CRUD。

Query Filter Documents中的Query filter(https://docs.mongodb.com/manual/reference/operator/query/#query-selectors):Query filter documents specify the conditions that determine which records to select for read, update, and delete operations.

use <field>:<value> expressions to specify the equality condition and query operator expressions.

{
  <field1>: <value1>,
  <field2>: { <operator>: <value> },
  ...
}

Specify AND ConditionsA compound query can specify conditions for more than one field in the collection’s documents. Implicitly, a logical AND conjunction connects the clauses of a compound query so that the query selects the documents in the collection that match all the conditions.

Specify OR Conditions :Using the $or operator, you can specify a compound query that joins each clause with a logical OR conjunction so that the query selects the documents in the collection that match at least one condition. 比如:

db.inventory.find( { $or: 
    [ { status: "A" }, 
      { qty: { $lt: 30 } } 
    ] 
} )

Specify AND as well as OR Conditions,同时指定 and 和 or 操作:

db.inventory.find( {
     status: "A",
     $or: [ { qty: { $lt: 30 } }, { item: /^p/ } ]
} )

Query on Embedded/Nested Documents

如何操作mongodb中的字典呢?

1:Match an Embedded/Nested Document:Equality matches on the whole embedded document require an exact match of the specified document, including the field order .

注意是完整匹配,包含字典元素的顺序,比如:

db.inventory.find(  { size: { w: 21, h: 14, uom: "cm" } }  )

2:Query on Nested Field

To specify a query condition on fields in an embedded/nested document, use dot notation .

使用点操作符,则其他的操作和单字段操作差不多,比如:

db.inventory.find( { "size.uom": "in" } )
db.inventory.find( { "size.h": { $lt: 15 } } )

Match an Array

mongodb中可以存储数组元素。

1:Match an Array:To specify equality condition on an array, use the query document { <field>: <value> } where <value> is the exact array to match, including the order of the elements.

完全匹配,包括数组中的元素顺序,比如:

db.inventory.find( { tags: ["red", "blank"] } )

If  wish to find an array that contains both the elements “red” and “blank”, without regard to order or other elements in the array, use the $all operator:

db.inventory.find( { tags: { $all: ["red", "blank"] } } )

通过 $all,匹配的元素不用考虑排序。

2:Query an Array for an Element

查询数组中的一个元素,To query if the array field contains at least one element with the specified value, use the filter { <field>: <value> } where <value> is the element value.

To specify conditions on the elements in the array field, use query operators.

db.inventory.find( { tags: "red" } )
db.inventory.find( { dim_cm: { $gt: 25 } } )

3:Specify Multiple Conditions for Array Elements

Query an Array with Compound Filter Conditions on the Array Elements,比如:

#只要有一个数组元素匹配就行
db.inventory.find( { dim_cm: { $gt: 15, $lt: 20 } } )

Query for an Array Element that Meets Multiple Criteria,查询满足多个条件的数组元素,比如:

db.inventory.find( { dim_cm: { $elemMatch: { $gt: 22, $lt: 30 } } } )

Use $elemMatch operator to specify multiple criteria on the elements of an array such that at least one array element satisfies all the specified criteria.

Query for an Element by the Array Index Position:

db.inventory.find( { "dim_cm.1": { $gt: 25 } } )

Query an Array by Array Length:

Use the $size operator to query for arrays by number of elements. For example, the following selects documents where the array tags has 3 elements.

db.inventory.find( { "tags": { $size: 3 } } )

Query an Array of Embedded Documents

如果数组中包含内嵌文档,如何操作?

1:Query for a Document Nested in an Array

查询数组中元素,Equality matches on the whole embedded/nested document require an exact match of the specified document, including the field order .

db.inventory.find( { "instock": { qty: 5, warehouse: "A" } } )

2:Specify a Query Condition on a Field in an Array of Documents

在数组元素中指定单个查询条件,比如:

db.inventory.find( { 'instock.qty': { $lte: 20 } } )
db.inventory.find( { 'instock.0.qty': { $lte: 20 } } )

3:Specify Multiple Conditions for Array of Documents

在数组元素中指定多个查询条件。

(1)A Single Nested Document Meets Multiple Query Conditions on Nested Fields

Use $elemMatch operator to specify multiple criteria on an array of embedded documents such that at least one embedded document satisfies all the specified criteria.

db.inventory.find( { "instock": { $elemMatch: { qty: 5, warehouse: "A" } } } )

如果没有$elemMatch,会怎么样?

(2)Combination of Elements Satisfies the Criteria

If the compound query conditions on an array field do not use the $elemMatch operator, the query selects those documents whose array contains any combination of elements that satisfies the conditions.

For example, the following query matches documents where any document nested in the instock array has the qty field greater than 10 and any document (but not necessarily the same embedded document) in the array has the qty field less than or equal to 20:

db.inventory.find( { "instock.qty": { $gt: 10,  $lte: 20 } } )

需要仔细体会。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK