MongoDB Syntax Practice

Source: Internet
Author: User

# # #简单操作过程

Basic query:

To construct the query data:

Db.test.insert ({name: "Stephen", Age:35,genda: "Male", email: "[email protected]"})

Db.test.insert ({name: "Stephen", Age:35,genda: "Male", email: "[email protected]"})

Db.test.insert ({name: "Stephen1", Age:35,genda: "Male", email: "[email protected]"})

Db.test.insert ({name: "Stephen", Age:36,genda: "Male", email: "[email protected]"})

Db.test.insert ({name: "Stephen", Age:37,genda: "Male", email: "[email protected]"})

--Multi-conditional query, equivalent to the where name of the SQL statement = "Stephen" and age = 35

Db.test.find ({name: "Stephen", Age:35})

{"_id": ObjectId ("59f1ae4388f3edba94091894"), "name": "Stephen", "Age": +, "Genda": "Male", "email": "[email Protec Ted] "}

--Returns the specified document key-value pair, returning only the name and age key-value pairs

Db.test.find ({},{name:1,age:1})

{"_id": ObjectId ("59f1ae4388f3edba94091894"), "name": "Stephen", "Age": 35}

{"_id": ObjectId ("59f1ae8588f3edba94091895"), "name": "Stephen", "Age": 36}

{"_id": ObjectId ("59f1ae8a88f3edba94091896"), "name": "Stephen", "Age": 37}

2. Query conditions

MongoDB provides a set of comparison operators: $lt/$lte/$GT/$gte/$ne, which in turn is equivalent to </<=/>/>=/!=

--Returns documents that meet the conditions of age >= && age <= 40

Db.test.find ({age:{$gte: +, $lte: 36}})

{"_id": ObjectId ("59f1ae4388f3edba94091894"), "name": "Stephen", "Age": +, "Genda": "Male", "email": "[email Protec Ted] "}

{"_id": ObjectId ("59f1ae8588f3edba94091895"), "name": "Stephen", "Age": $, "Genda": "Male", "email": "[email Protec Ted] "}

--Returns the document of the specified key-value pair condition age >= && Age <= 40

Db.test.find ({age:{$gte: +, $lte: 36}},{name:1,age:1})

{"_id": ObjectId ("59f1ae4388f3edba94091894"), "name": "Stephen", "Age": 35}

{"_id": ObjectId ("59f1ae8588f3edba94091895"), "name": "Stephen", "Age": 36}

--Return condition matches name! = "Stephen1"

Db.test.find ({name:{$ne: "Stephen"}})

{"_id": ObjectId ("59f1b20588f3edba94091898"), "name": "Stephen1", "age": +, "Genda": "Male", "email": "[Email Prote CTED] "}

--$in equivalent to in in SQL, the following example is equivalent to the in ("Stephen", "Stephen1") of SQL

Db.test.find ({name:{$in: ["Stephen", "Stephen2"]}})

--The following example is equivalent to name = "Stephen1" or age = 35

Db.test.find ({$or: [{name: ' Stephen1 '},{age:36}]})

{"_id": ObjectId ("59f1ae8588f3edba94091895"), "name": "Stephen", "Age": $, "Genda": "Male", "email": "[email Protec Ted] "}

{"_id": ObjectId ("59f1b20588f3edba94091898"), "name": "Stephen1", "age": +, "Genda": "Male", "email": "[Email Prote CTED] "}

--The following example shows how to mix $or with $in

Db.test.find ({$or: [{name:{$in: ["Stephen1", "Setphen12"]}},{age:36}]})

3. Query of the null data type

--when querying with null data, all values are null, and documents that do not include the specified key are retrieved

Db.test.find ({"x": null})

Db.test.find ({a:null})

{"_id": ObjectId ("59f1ae4388f3edba94091894"), "name": "Stephen", "Age": +, "Genda": "Male", "email": "[email Protec Ted] "}

{"_id": ObjectId ("59f1ae8588f3edba94091895"), "name": "Stephen", "Age": $, "Genda": "Male", "email": "[email Protec Ted] "}

{"_id": ObjectId ("59f1ae8a88f3edba94091896"), "name": "Stephen", "Age": Notoginseng, "Genda": "Male", "email": "[email Protec Ted] "}

{"_id": ObjectId ("59f1b1fd88f3edba94091897"), "name": "Stephen", "Age": +, "Genda": "Male", "email": "[email Protec Ted] "}

{"_id": ObjectId ("59f1b20588f3edba94091898"), "name": "Stephen1", "age": +, "Genda": "Male", "email": "[Email Prote CTED] "}

--and then, by $exists, to determine whether a specified key exists.

Db.test.find ({x:{$in: [null], $exists: true}})

{"_id": ObjectId ("59f1c40b88f3edba94091899"), "X": null}

4. Regular query

Db.test.find ()

--Regular grammar

Db.test.find ({name:/1/})

{"_id": ObjectId ("59f1b20588f3edba94091898"), "name": "Stephen1", "age": +, "Genda": "Male", "email": "[Email Prote CTED] "}

-I indicates ignoring case

Db.test.find ({name:/stephen?/i})

5. Array data query

Db.wqq.insert ({name:["AA", "BB", "CC"]})

Db.wqq.insert ({name:["AA", "ee", "CC"]})

Db.wqq.insert ({name:["ff", "EE", "CC"]})

--all documents containing banana in the array will be retrieved.

Db.wqq.find ({name: "AA"})

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["AA", "BB", "CC"]}

{"_id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["AA", "ee", "CC"]}

--Retrieve the case where multiple elements are required in the array, using $all. In the following example, the array must contain both the FF and EE

Db.wqq.find ({name:{$all: ["FF", "CC"]}})

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["FF", "EE", "CC"]}

--Exact match is the retrieved document, the array data in the name value must exactly match the query criteria, i.e. no more, no less, and the order must be consistent

Db.wqq.find ({name:["ff", "EE", "CC"]})

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["FF", "EE", "CC"]}

--matches the value of the specified subscript element in the array, the starting subscript of the array is 0

Db.wqq.find ({"name.1": "BB"})

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["AA", "BB", "CC"]}

--You can get the length of the array through $size, but $size cannot be used in conjunction with comparison operators

Db.wqq.find ({"name": {$size: 3}})

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["AA", "BB", "CC"]}

{"_id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["AA", "ee", "CC"]}

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["FF", "EE", "CC"]}

--If you need to retrieve the result of size > N, you cannot use $size directly, only add an extra key to represent the element data in the data, and when you manipulate the elements in the data, you need to update the value of the size key at the same time

--construct data for subsequent experiments

Db.wqq.update ({}, {"$set": {"size": 3}},false,true)

Writeresult ({"nmatched": 3, "nupserted": 0, "nmodified": 3})

> Db.wqq.find ()

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["AA", "BB", "CC"], "Size": 3}

{"_id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["AA", "ee", "CC"], "Size": 3}

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["FF", "EE", "CC"], "Size": 3}

--Each time you add a new element, you have to increment the size of the atom one time

Db.wqq.update ({},{$push: {name:123}, $inc: {size:1}},0,1)

Writeresult ({"nmatched": 3, "nupserted": 0, "nmodified": 3})

> Db.wqq.find ()

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["AA", "BB", "CC", 123], "Size": 4}

{"_id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["AA", "ee", "CC", 123], "Size": 4}

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["FF", "EE", "CC", 123], "Size": 4}

--Returns some of the data in the array by $slice. "$slice": 2 represents the first two elements in an array

Db.wqq.find ({},{name:{$slice: 2},size:0})//Negative is the last 2 elements

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["AA", "BB"]}

{"_id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["AA", "EE"]}

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["FF", "EE"]}

--$slice: [2,1], which means to take 1 from the second 2 element, and take all subsequent data if you get the number of elements after the number greater than 2

Db.wqq.find ({},{name:{$slice: [2,1]}})

Db.wqq.find ({},{name:{$slice: [2,1]},size:0})

{"_id": ObjectId ("59f1c60388f3edba9409189c"), "name": ["CC"]}

{"_id": ObjectId ("59f1c6b988f3edba9409189d"), "name": ["CC"]}

{"_id": ObjectId ("59f1c6ba88f3edba9409189e"), "name": ["CC"]}

6. Embedded document Query

--When an embedded document is an array, the $elemmatch operator is required to help locate an element match, otherwise the embedded file will be fully matched

--that is, you need to list all the elements as query criteria before you can

Db.aa.insert ({comments:[{author: "Sharesoe", Score:3},{author: "Mary", Score:6}]})

Writeresult ({"ninserted": 1})

> Db.aa.find ()

{"_id": ObjectId ("59f1cc8e88f3edba9409189f"), "comments": [{"Author": "Sharesoe", "Score": 3}, {"Author": "Mary" , "Score": 6}]}

--

Db.aa.find ({comments:{$elemMatch: {author: "Sharesoe", score:{$gte: 3}}})

{"_id": ObjectId ("59f1cc8e88f3edba9409189f"), "comments": [{"Author": "Sharesoe", "Score": 3}, {"Author": "Mary" , "Score": 6}]}


This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1976524

MongoDB Syntax Practice

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.