Mongovue Query Two

Source: Internet
Author: User

1. Assume that the following records are available:

{"_id": ObjectId ("51d7b0d436332e1a5f7299d6"), "name": {"First": "Barack", "Last": "Obama"}}

To find out that first barack,last was the Obama record.

Precise representation of the key of an inline document by dot notation

Db.profile.find ({"Name.first": "Barack", "Name.last": "Obama"});


2. In more complex cases, when the value is an array, use the $elemmatch operator, for example:

{

"Content": "...",

"Comment": [

{

"Author": "Zhangsan",

"Score": 3,

"Comment": "shafa!"

},

{

"Author": "Lisi",

"Score": 5,

"Comment": "lzsb!"

}

]

}

To query for records with a author of Zhangsan,score of 3,

If you use {"Comment.author": "Zhangsan", "Comment.score": {"$gte": 4}} will also print this, this is not what we want, can be understood to take the value of the Author,score, not the same person's value, That's what's causing the results.

Use $elemmatch to pin to the value of an element,

Execute: Db.blogs.find ({"comment": {"$elemMatch": {"author": "Zhangsan", "score": {"$GT": 4}}}), no results returned,

Execute Db.blogs.find ({"comment": {"$elemMatch": {"author": "Zhangsan", "score": {"$GT": 2}}}), displaying a result.


3. The key value is a null value query operation

{sex:{$in: [null], $exists: true}}, the query has a sex field and is a record of the [null] value

{Sex:null}, query for records with an empty sex field, including records that do not exist in the sex field

4. $all, matching the key values of those specified keys contains the array, and the array contains the documents for all elements of the specified array, and the order of elements in the array does not affect the query results.


Syntax: {field: {$all: [<value>, <value1> ...]}


Record: {"name": "T2", "Amount": "Tags": ["Appliances", "school", "book"]}

Querying for records with the second element as a school

Db.inventory.find ({"Tags.1": "School"})

5. $size, use its query to specify the length of the array.

Syntax: {field: {$size: Value}}

{Tags: {$size: 3}}, querying the tags field contains 3 elements of a record

6. $or, $in, $nor, $nin

{"Name": {"$in": ["Zhangsan", "Mike"]}}, the query name is Zhangsan, or Mike's record

{"$or": {"name": "Zhangsan"},{"Age": 12}}


7. Regular expressions, fuzzy queries

{"Name":/mike?/i}, returning a record with the name Mike or Mike1

Add an element to the array

{"$push": {"Ziliao": "Weight"}}

$slice, take the first few elements of the array

8, delete field, $unset, for update operation,

9. Query the fields of the specified type, $type

{"Date": {"$type": 18}} Query the Date field Int64 records

{"Date": {"$type": 9}} query the date field for a record of date

Attached: Bson and ID table


Mongovue Query Two

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.