MongoDB Query operation

Source: Internet
Author: User
Tags mongodb mongodb query

Query by criteria

Compare operations:$lt, $lte, $gt, $gte, $ne

Db.user.find ({"score": {$gt:}});

$or : Contains multiple conditions, the relationship between them for or, $nor equivalent to or take the inverse

Db.user.find ({$or: [{"score": {$gt:}},{"user_id") :"U3"}]});

$not: used as the other condition, take the inverse

$mod: Divides the value of the query by the first given value, if the remainder equals equal to two values, the match succeeds

Db.user.find ({"score": {$mod: [4,1]}});

$in : Query Multiple values for a key, as long as the key matches one of them, $nin to not contain

Db.user.find ({"score": {$ in: [+]}});

$all : The key needs to match all the values

Db.user.find ({"House": {$all: [All]}});

$exists: checks if a key exists, 1 is present, and 0 indicates that it does not exist

Db.user.find ({"name": {$exists: 1}});

NULL Type: not only can match the value of the key is null, but also match the condition that the key does not exist

Db.user.find ({"name":null});

Querying arrays

a single element matches , just like the previous write condition, {key:value}

Db.user.find ({"House":);

multiple elements match , using $all, {key:{$all: [A, B]}}, the order of the elements does not matter

Db.user.find ({"House": {$all: [+]}});

You can use an index to specify a specific location for the query array, {"Key. Index number": Value}

Db.user.find ({"house.1":+);

Query an array of length, using the $size

Db.user.find ({"House": {$size:2}});

Specify a subset, use a $slice, positive numbers are the number of front bars, negative numbers are the number of tails, you can specify the offset and how many elements to return, such as: $slice: [50,10]

Db.user.find ({"House": {$exists:1}},{" House ": {$slice: [0,2]}});

You can use $ to specify any of the array elements that match the criteria, such as: {"users.$": 1}

Db.user.find ({"House": {$exists:1}},{"house.$": 1});

$elemMatch: Requires multiple conditional statements to be used to compare an array element

Db.user.find ({"House": {$elemMatch: {$gte:$, $lt:+}});

command to query the number of record bars: Count

Db.user.find (). Count ();

If you want to get the number of bars logged after a conditional query, you need to specify count (True or not 0)

    • command to limit the number of record entries returned: limit (number of bars to return)
    • command to limit the starting point of the number of records returned: Skip (return from the first few)
    • Sorted Commands: Sort ({fields to sort: 1 for ascending,-1 for descending})
Db.user.find ({}). Limit (2). Count (1);

Db.user.find ({}). Skip (1). Limit (2);

Db.user.find ({}). Sort ({"Score": 1});

Paged query: Combining Limit,skipt and sort
Of course, you can also use other means of paging, such as using a custom ID, and then pagination according to the ID


Querying all non-duplicated data for a given key, command: DISTINCT
Syntax: Db.runcommand ({"DISTINCT": Collection name, "Key": "field with no duplicate data"});

Db.runcommand ({"distinct":"user","key ":"user_id"});

Stored Procedures
MongoDB's stored procedure is actually a custom JS function.
Use Db.system.js.save ({"_id": Name, Value: function});

var return A +b;}; Db.system.js.save ({"_id":"myF","value  ": Totalf});

Can be viewed by the following command: Db.system.js.find ();
Can be called by the following command: Db.eval (name)

Db.eval ("MyF (1,3)");

MongoDB Query operation

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.