mongodb query
query document 結構
db.collection.find( <query filter>, <projection> )a query filter: specify which documents to return.
a query projection : specifies which fields from the matching documents to return. The projection limits the amount of data that MongoDB returns to the client over the network.
You can optionally add a cursor modifier to impose limits, skips, and sort orders. The order of documents returned by a query is not defined unless you specify a sort().
Query on Embedded Documents
Exact Match
To specify an exact equality match on the whole embedded document, use the query document { : } where is the document to match. Equality matches on an embedded document require an exact match of the specified , including the field order.
Equality Match on Fields
db.users.find( { "favorites.artist": "Picasso" } )Query on Arrays
When the field holds an array, you can query for an exact array match or for specific values in the array
If you specify multiple conditions without using the $elemMatch operator, then some combination of the array elements, not necessarily a single element, must satisfy all the conditions; i.e. different elements in the array can satisfy different parts of the conditions
exact match
Equality matches on the array require that the array field match exactly the specified , including the element order
db.users.find( { badges: [ "blue", "black" ] } )Match an Array Element
Equality matches can specify a single element in the array to match. These specifications match if the array contains at least one element with the specified value
db.users.find( { badges: "black" } )Match a Specific Element of an Array
badges is an array that contains "black" as the first element:
db.users.find( { "badges.0": "black" } )Specify Multiple Criteria(標準) for Array Elements
Single Element Satisfies the Criteria
$elemMatch : at least one array element
db.users.find( { finished: { $elemMatch: { $gt: 15, $lt: 20 } } } )Combination of Elements Satisfies the Criteria
e.g., one element can satisfy the greater than 15 condition and another element can satisfy the less than 20 condition, or a single element can satisfy both
db.users.find( { finished: { $gt: 15, $lt: 20 } } )使用操作符
Comparison
小于等于$lte
大于 $gt
大于或等于$gte
不等于$ne
$in 在數組里
$nin 不在數組里
logical
$or
$and 逗號也表示$and
$not
$nor returns all documents that fail to match both clauses
Element
$exists 存在這個field
$type 類型匹配
Evaluation(計算)
$mod 模運算
$regex regular expression.
$text Performs text search.
$where satisfy a JavaScript expression.
Array
$all contain all elements specified in the query.
$elemMatch
$size 元素個數
comments
$comment Adds a comment to a query predicate.
associates a comment to any expression taking a query predicate.
for example:
轉載于:https://www.cnblogs.com/jcuan/p/5698872.html
總結
以上是生活随笔為你收集整理的mongodb query的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【iOS基础知识】const与宏的区别
- 下一篇: Oulipo(kmp算法)