Mongo CRUD data operator summary, javascrud

Source: Internet
Author: User
Tags findone mongo shell

Mongo CRUD data operator summary, javascrud

Comparison

Name Description
$ Eq Equal
$ Gt Greater
$ Gte Greater than or equal
$ Lt Less
$ Lte Less than or equal
$ Ne Not equal
$ In In the condition
$ Nin Not in the condition

Logic

Name Description
$ Or Or
$ And And
$ Not Non
$ Nor Neither nor, nor: [{name: 'bucketall', age: 12}]. The name is not 'bucketall', and the age is not 12.

Element

Name Description
$ Exists Whether the field exists
$ Type Field Type

Array

Name Description
$ All Obtain all data that meets the conditions
$ ElemMatch Obtain all the data that meets the conditions.
$ Size If the returned value is an array, define the number of returned array elements.

Remarks

Name Description
$ Comment Add a description or remarks to the command.

"

$ Where

db.myCollection.find( { $where: "this.credits == this.debits" } );db.myCollection.find( { $where: "obj.credits == obj.debits" } );db.myCollection.find( { $where: function() { return (this.credits == this.debits) } } );db.myCollection.find( { $where: function() { return obj.credits == obj.debits; } } );
Where accepts parameters in two forms of string functions. Some functions are also provided.
assert()BinData()DBPointer()DBRef()doassert()emit()gc()HexData()hex_md5()isNumber()isObject()ISODate()isString()Map()MD5()NumberInt()NumberLong()ObjectId()print()printjson()printjsononeline()sleep()Timestamp()tojson()tojsononeline()tojsonObject()UUID()version()

Note:

Because the execution plan generated by mongo after $ where is used cannot use indexes well, we do not recommend that you use $ where

$ All

Select all data that meets the conditions ).
    { <field>: { $all: [ <value1> , <value2> ... ] } }
$ All has the same functions as $ and, for example, $ and:
    { $and: [ { tags: "ssl" }, { tags: "security" } ] }

$ And

    { $and: [ { tags: "ssl" }, { tags: "security" } ] }

$ BatchSize

db.inventory.find().batchSize(10)
Control the number of data records returned. Note that the batchSize () setting does not take effect in mongo shell. At the same time, distinct () does not take effect.

$ Box

db.places.find( {   loc: { $geoWithin: { $box:  [ [ 0, 0 ], [ 100, 100 ] ] } }} )
Define a "box" with two points to determine the position of a "box" (we understand the box as a rectangle of a two-dimensional plane ), the above sentence means to find the points in the box, [], [], [], [], and []. All these values will be returned.

$ Center

    db.places.find(   { loc: { $geoWithin: { $center: [ [-74, 40.74], 10 ] } } })
Returns all points in the circular area (Center Coordinate is (-74, 40.74), and circle radius is 10)

$ Comment

    db.collection.find( { $query: { <query> }, $comment: <comment> } )
It can be understood that each execution adds a note, which will be recorded in the profile, which helps to troubleshoot problems in the future.

$ Count

    db.collection.find( { a: 5, b: 5 } ).count()
Count

$ Distinct

    db.collection.distinct(field, query)
Deduplication

$ ElemMatch

    { <field>: { $elemMatch: { <query1>, <query2>, ... } } }    { _id: 1, results: [ 82, 85, 88 ] }    { _id: 2, results: [ 75, 88, 89 ] }    db.scores.find(       { results: { $elemMatch: { $gte: 80, $lt: 85 } } }    )
Satisfying the first interval

$ Exists

    { field: { $exists: <boolean> } }    db.records.find( { a: { $exists: true } } )    result:        { a: 5, b: 5, c: null }        { a: 3, b: null, c: 8 }        { a: null, b: 3, c: 9 }        { a: 1, b: 2, c: 3 }        { a: 2, c: 5 }        { a: 3, b: 2 }        { a: 4 }    db.records.find( { b: { $exists: false } } )    result:        { a: 2, c: 5 }        { a: 4 }        { c: 6 }

$ Find

    query.find({ name: 'Los Pollos Hermanos' }).find(callback)
Search for all qualified data

$ FindOne

    query.findOne({ name: 'Los Pollos Hermanos' }).find(callback)
Returns a data that meets the condition.

$ FindAndModify

    {          findAndModify: <collection-name>,          query: <document>,          sort: <document>,          remove: <boolean>,          update: <document>,          new: <boolean>,          fields: <document>,          upsert: <boolean>    }
Field Description
Query A common request statement may obtain multiple data records, but findAndModify only processes the first data entry.
Sort Because findAndModify can only process one piece of data, we usually sort the data obtained by the query.
Remove If true, the first data record after the query is removed. The default value is false.
Update The first entry after the query is updated
New When set to true, the modified data instead of the original data is returned. The default value is false.
Fields Set Group
Upsert When the value is true, the updated value is updated if it exists, and inserted if it does not exist. The default value is false.
db.people.findAndModify( {   findAndModify: "people",   query: { name: "Gus", state: "active", rating: 100 },   sort: { rating: 1 },   update: { $inc: { score: 1 } },   upsert: true,   new : true} );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.