MongoDB (5) Document CRUD operations

Source: Internet
Author: User
Tags mongodb
MongoDB Getting Started column

Http://blog.csdn.net/column/details/19681.html


MongoDB document CRUD Operations
Querying documents The basic syntax for querying a document using MongoDB is as follows:

Db.collection_name.find (query, projection) # Return data in compressed format
Db.collection_name.find (query, projection). Pretty () # return data in easy-to-read format
Query: Optional, use the query operator to specify the criteria;
Projection: optional, use the projection operator to specify the returned key, and if you need to return all keys in the document, simply omit the parameter;
# Querying all documents in the Testdb.artciles collection
> Use TestDB
> Db.articles.find (). Pretty ()
# Querying documents author= ' Assad ' in the Testdb.articles collection
> Db.atricles.find ({author: ' Assad '})

Query condition operator

Operator

Meaning and syntax Example
Compare Query characters
: equals (=);
{<key>:<value>}
Db.col.find ({name: ' Assad '})
Querying documents for name= ' Assad '
$ne Not equal to (! =);
{<key>:{$ne: <value>}}
Db.col.find ({name:{$ne: ' Assad '}})
Querying documents for name!= ' Assad '
$lt Less Than (<):
{<key>:{$lt: <value>}}
Db.col.find ({score:{$lt: 120}})
Querying documents for SCORE>120
$gt Greater than (>);
{<key>:{$gt: <value>}}
Db.col.find ({score:{$gt: 120}})
Querying documents for SCORE<120
$lte Less than or equal to (<=);
{<key:{$lte: <value>}}
Db.col.find ({score:{$lte: 120}})
Querying documents for SCORE<=120
$gte Greater than or equal to (>=);
{<key>:{$gte: <value>}}
Db.col.find ({score:{$gte: 120}})
Querying documents for score>= 120
Logical relation Query character
$and and and relationships;
{$and: [Query1, Query2]}
{Query1, Query2}
Db.col.find ({$and: [{city: ' Guangzhou '}, {score:{$gt: 250}}]})
Db.col.find ({city: ' Guangzhou ', score:{$gt: 250}}
Query city= ' guangzhou ' and core>250 documents
$or or or relationship;
{$or: [Query1, Query2]}
Db.col.find ({$or: [{city: ' Guangzhou '}, {score:{$gt: 250}}]})
Querying documents city= ' Guangzhou ' or core>250
$nor nor XOR or relationship;
{$nor: [Query1, Query2]}
Db.col.find ({$nor: [{city: ' Guangzhou '}, {score:{$gt: 250}}]})
Documents for querying (city= ' Guangzhou ' or core<=250) and (city!= ' Guangzhou ' and core > 250)
$not Non-relationship;
{$not: {query}}
Db.col.find ({$not: {city: ' Guangzhou '}})
Querying documents for city!= ' Guanhgzhou '
Member Relations Query character
$all The query key matches the result of all the members in the specified array;
{<key>:{$all: [Array]}}
Db.col.find ({tages:{$all: [' Java ', ' CPP ', ' Linux '}})
Query tages field array containing ' Java ', ' CPP ', ' Linux ' all these values of the document
$in The query key matches the result of any of the members in the specified array;
{<key>:{$in: [Array]}}
Db.col.find ({tages:{$in: [' Java ', ' CPP ', ' Linux '}})
Query a tages field array containing ' Java ', ' CPP ', and ' Linux ' for any of the values in the document
$nin The query key does not match the result of any of the members in the specified array;
{<key>:{$in: [Array]}}
Db.col.find ({tages:{$in: [' Java ', ' CPP ', ' Linux '}})
Query Tages field array does not contain ' Java ', ' CPP ', ' Linux ' documents
. Query key to specify subscript member, index starting from 0;
{<key.index>:query}
Db.col.find ({tages.1: ' java '})
Query tages field Array 2nd element = ' Java ' document
Value Property Query character
$size Querying arrays of specified lengths
{<key>:{$size: value}}
Db.col.find ({tages:{$size: 3}})
Querying a document with a length of 3 tages field array can be combined with a comparison query
$type To query the specified type of key, see the list of specific types:
Https://docs.mongodb.com/manual/reference/operator/query/type/index.html
{<key>: {$type: TypeCode}}
Db.col.find ({title:{$type: 2}})
Querying a document with the title type String
$exits Querying a document that specifies the key for which the condition exists
{<key>:{$exists: <boolean>}}
Db.col.find ({school:{$exits: false}})
Querying all documents that do not exist in the school field
Null is not actually an operator, it is a placeholder for null values
{<key>:null}
{<key>:{$in: [NULL]}}
Db.col.find ({school:null})
The Query school field does not exist, or the school field value is empty document
Db.col.find ({school:{$in: [null], $exists: true}})
Query school field exists, but the value is empty document
$regex Regular matching of strings, using Perl-compatible expressions, can be used to achieve similar SQL like clause effect;
{<key>:{$regex:p attern, $options: Ops}}

Where $options is used to decorate the regex with the following parameters:
-I: Ignore case;
-X: Force a branch to a string without a callout \ n;
-The dot in s:pattern matches all characters (including line break);
-X: Ignores special characters that are not escaped in pattern;
Db.col.find ({name:{$regix: "^a*"}})
Db.col.find ({name:/^a*/}})
Querying a document whose name begins with a

Db.col.find ({name:{$regex: "^a*", $options: ' I '}})
Db.col.find ({name:/^a*/i})
Query the name of a document that starts with a, or is slightly case-sensitive
$where Use any JavaScript as part of the query, including JS expressions and JS closures;
{$where: Javascript-operation}
In the JS expression, use This,obj to refer to each Document object
Db.col.find ({$where: "This.score > This.salary"})
Db.col.find ({$where: "Obj.score = this.salary"})
Db.col.find ({$where: function () {return (This.score > This.salary)}})
Querying all score > Salary documents in col;

> Use TestDB
# Querying documents author= "Assad" in the Testdb.articles collection
> Db.articles.find ({author: ' Assad '})
# Find documents for likes > 1000
> Db.articles.find ({likes:{$gt: 1000}})
# Query for documents with likes greater than 1000, less than 1500
> Db.artciles.find ({likes:{$gt: $, $LT: 1500}})
# query Author= "Assad" simultaneously likes > 2000 documentation
> Db.articles.find ({author: ' Assad ', likes:{$gt: 2000}})
# Querying documents authoer= "Assad" or "vancy"
> Db.articles.find ({author:{$in: [' Assad ', ' vancy ']}})
# query Tages array containing ' Java ', ' Groovy ' documents
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.