MongoDB crud R

Source: Internet
Author: User

Document Query
Command Operation
db.mycoll.find(key:value) Data for query condition
db.mycoll.find(...).count() Querying and Counting
db.mycoll.find(...).limit(n) Limit the number of query bars
db.mycoll.find(...).skip(n) Skip a query
db.mycoll.find(...).sort(...) Query and sort
db.mycoll.find(...).pretty() Querying and optimizing output results
Read View all
25 > db.set2test.find(){ "_id" : ObjectId("5a7943273fdd5fec85460638"), "x" : 3 }{ "_id" : ObjectId("5a7943273fdd5fec85460639"), "x" : 4 }{ "_id" : ObjectId("5a7943273fdd5fec8546063a"), "x" : 5 }.......{ "_id" : ObjectId("5a7943273fdd5fec8546064b"), "x" : 22 }Type "it" for more26 > it{ "_id" : ObjectId("5a7943273fdd5fec8546064c"), "x" : 23 }{ "_id" : ObjectId("5a7943273fdd5fec8546064d"), "x" : 24 }{ "_id" : ObjectId("5a7943273fdd5fec8546064e"), "x" : 25 }

Enter it to see more data

Specify fields
44 > db.unicorns.find({},{_id: 0, name: 1, gender: 1, weight: 1}){ "name" : "Aurora", "gender" : "f", "weight" : 450 }{ "name" : "Horny", "weight" : 600, "gender" : "m" }{ "name" : "Aurora", "weight" : 450, "gender" : "f" }{ "name" : "Unicrom", "weight" : 984, "gender" : "m" }{ "name" : "Roooooodles", "weight" : 575, "gender" : "m" }{ "name" : "Solnara", "weight" : 550, "gender" : "f" }{ "name" : "Ayna", "weight" : 733, "gender" : "f" }{ "name" : "Kenny", "weight" : 690, "gender" : "m" }{ "name" : "Raleigh", "weight" : 421, "gender" : "m" }{ "name" : "Leia", "weight" : 601, "gender" : "f" }{ "name" : "Pilot", "weight" : 650, "gender" : "m" }{ "name" : "Nimue", "weight" : 540, "gender" : "f" }{ "name" : "Dunx", "weight" : 704, "gender" : "m" }
Conditional query
45 > db.unicorns.find({weight: {$gt: 700}},{_id: 0, name: 1, gender: 1, weight: 1}){ "name" : "Unicrom", "weight" : 984, "gender" : "m" }{ "name" : "Ayna", "weight" : 733, "gender" : "f" }{ "name" : "Dunx", "weight" : 704, "gender" : "m" }
Size
operator meaning symbols
$gt Greater than >
$gte GT equal >=
$lt Less than <
$lte Lt equal <=
$ne Not equal !=
$eq Equal =
41 > db.unicorns.find({weight: {$gt: 700}}).count()3
Logic
operator meaning symbols
$or Or ||
$and and &&
48 > db.unicorns.find({    $and:     [{weight: {$gt: 700}},{gender: 'f'}]    }, {    _id: 0,     name: 1,     gender: 1, weight: 1    

Equivalent to

49 > db.unicorns.find(    {        weight: {$gt: 700},gender: 'f'        },     {        _id: 0, name: 1, gender: 1, weight: 1        }){ "name" : "Ayna", "weight" : 733, "gender" : "f" }
Fuzzy
    • Regular
54 > db.unicorns.find({name: {$regex: /^[A-D]/}}, {_id: 0, name: 1}){ "name" : "Aurora" }{ "name" : "Aurora" }{ "name" : "Ayna" }{ "name" : "Dunx" }
    • Whether the key value exists or not
61 > db.unicorns.find({ loves: { $exists: false } }, {_id: 0}){ "name" : "Aurora", "gender" : "f", "weight" : 450 }
    • Range
64 > db.unicorns.find({ name: { $in: ["Ayna", "Aurora"] } }, {_id: 0, name: 1, gender: 1, weight: 1}){ "name" : "Aurora", "gender" : "f", "weight" : 450 }{ "name" : "Aurora", "weight" : 450, "gender" : "f" }{ "name" : "Ayna", "weight" : 733, "gender" : "f" }

Similarly, there are$nin

More query operators see here

Output optimization
28 > db.unicorns.find({gender:'f'}).pretty(){    "_id" : ObjectId("5a793d993fdd5fec8546062b"),    "name" : "Aurora",    "gender" : "f",    "weight" : 450}{    "_id" : ObjectId("5a79408c3fdd5fec8546062d"),    "name" : "Aurora",    "dob" : ISODate("1991-01-24T05:00:00Z"),    "loves" : [        "carrot",        "grape"    ],    "weight" : 450,    "gender" : "f",    "vampires" : 43}......
Sort Ascending
30 > db.unicorns.find().sort({weight:1}){ ... "weight" : 421,... }{ ... "weight" : 450 }{ ... "weight" : 450, "gender" : "f", "vampires" : 43 }{ ... "weight" : 540, "gender" : "f" }{ ... "weight" : 550, "gender" : "f", "vampires" : 80 }{ ... "weight" : 575, "gender" : "m", "vampires" : 99 }{ ... "weight" : 600, "gender" : "m", "vampires" : 63 }{ ... "weight" : 601, "gender" : "f", "vampires" : 33 }{ ... "weight" : 650, "gender" : "m", "vampires" : 54 }{ ... "weight" : 690, "gender" : "m", "vampires" : 39 }{ ... "weight" : 704, "gender" : "m", "vampires" : 165 }{ ... "weight" : 733, "gender" : "f", "vampires" : 40 }{ ... "weight" : 984, "gender" : "m", "vampires" : 182 }
Descending
31 > db.unicorns.find().sort({weight:-1}){ ... "weight" : 984, "gender" : "m", "vampires" : 182 }{ ... "weight" : 733, "gender" : "f", "vampires" : 40 }{ ... "weight" : 704, "gender" : "m", "vampires" : 165 }{ ... "weight" : 690, "gender" : "m", "vampires" : 39 }{ ... "weight" : 650, "gender" : "m", "vampires" : 54 }{ ... "weight" : 601, "gender" : "f", "vampires" : 33 }{ ... "weight" : 600, "gender" : "m", "vampires" : 63 }{ ... "weight" : 575, "gender" : "m", "vampires" : 99 }{ ... "weight" : 550, "gender" : "f", "vampires" : 80 }{ ... "weight" : 540, "gender" : "f" }{ ... "weight" : 450 }{ ... "weight" : 450, "gender" : "f", "vampires" : 43 }{ ... "weight" : 421, "gender" : "m", "vampires" : 2 }
Paging Query
37 > db.unicorns.find().sort({weight:-1}).limit(2).skip(10).pretty(){    "_id" : ObjectId("5a793d993fdd5fec8546062b"),    "name" : "Aurora",    "gender" : "f",    "weight" : 450}{    "_id" : ObjectId("5a79408c3fdd5fec8546062d"),    "name" : "Aurora",    "dob" : ISODate("1991-01-24T05:00:00Z"),    "loves" : [        "carrot",        "grape"    ],    "weight" : 450,    "gender" : "f",    "vampires" : 43}
Count
39 > db.unicorns.find({gender: 'f'}).count()6
Go heavy
67 > db.unicorns.distinct("gender")[ "f", "m" ]

MongoDB crud R

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.