MongoDB Command Guide

Source: Internet
Author: User
Tags mongodb find mongodb update

I. What is MongoDB

    simply said mongodb is a kind of database, like our common mysql,sqlserver,orcale. But unlike these databases, they are relational databases, and mongodb is a non-relational database (NOSQL). mongodb is a high performance , open source, modeless document database, is the current nosql Comparison of the popular one in the database. It can be used in many scenarios to replace the traditional relational database or key/value storage methods. MONGO is developed using C + +. MONGO's official website address is: http://www.mongodb.org/, where readers can get more detailed information.

MongoDB has three basic terms, a database, a collection, and a document. Database we all know what it is. So what is a collection, which is the equivalent of a MySQL table; a document is equivalent to a column in a table.

That's quite clear.

First, the instructions for the database and the collection:

1. View all the databases:

Show DBS

2. Use a database

Use MyDB

3. View all the collections in this database:

Show collections


Two. Query: MongoDB find


db.collection.find (query, fields)
Query is optional. Using query operators to specify query criteria
Fields are optional. Specifies the key that is returned. Returns all key values in a document when queried, simply omit the argument (omitted by default)


1). Query all documents in the collection.


//will return all documents in the collection
Db.collection.find ()
//or
db.collection.find ({})


when the first parameter is a key/value pair, the query process means that conditional filtering (query) is performed, and the following query returns a collection of documents with an age key value of 18 in the collection collection.


Db.collection.find ({"Age": +})
//can also be multiple criteria query, comma separated just fine
Db.collection.find ({"Age": +, "sex": "Man"})


We can specify the returned key by using the second parameter of find.


If find does not specify a second argument, the query operation returns all the key values in the query document by default. The returned key can also be specified in MONGO so that we can avoid querying for unused key values, saving the amount of data transferred and memory consumption.


db.users.find ({}, {"Age": 1})
Note {"Age": 1} does not refer to age=1 because it is in the second argument (fields), which refers to finding all of the age in a document, and of course, multiple values.




If the data is double-layered. For example:
"User": {
"name": "MM",
"Age": Ten
}


Both query and fields are supported. For example, you need to query all players of age = 10.
Db.collection.find ({"User.age":)


the query criteria are not just equal, but can also be used for other comparison operations.
For example, we use the following comparison operator "$gt", "$gte", "$lt", "$lte", "$ne" (corresponding to ">", ">=", "<", "<=", "! ="), which are grouped together for range lookups.
If you are searching for players older than 20 and younger than 30
Db.collection.find ({"Age": {"$gt": +, "LT": ()})


$in


find the key in the values list of the document.


Db.collection.find ({"Age": {"$in": [10,20]}})


MongoDB is very flexible, all of the above queries, can be arbitrary combination, can also be used for update, start your imagination, as long as the MONGODB syntax to meet.






three. Update: MongoDB update
The MongoDB update uses an updated document.


db.collection.update (criteria, objnew, Upsert, Multi)


query : Queries for update, similar to where in SQL update query
fields:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
Upsert: This parameter means that if there is no record of update in the document, insert objnew,true as INSERT, default is False, do not insert.
Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated.



Upsert and multi are simply not explained. Here's how to talk about query and fields:


1) $set
usage: {$set: {field:value}}
is the equivalent of SQL set field = value, all data types support $set.


db.collection_name.update ({"id": 1}, {$set: {"name": "MM"}})
Set is the value "mm" that sets key to name


2) $inc
usage: {$inc: {field:value}}

meaning to a Number field field adds value, for example:


db.collection_name.update ({"id": 1}, {$set: {"number": 1}})
as above, if there is no name or number but needs to be added (not even the "id" =1 doc), Upsert must be set to true.


3) $unset
usage: {$unset: {field:1}}


as the name implies, the field is deleted. Example:
> Db. collection_name. Update ({"id": 1} , {$unset: {"name": 1}})
is to delete the doc's name field.

do not see field:1 inside of 1 is what to use.


Array operations:


4) $push


Usage: {$push: {Field:value}}


Append value To field, field must be array type, if field does not exist, a new array type will be added. Cases:


> DB. collection_name. Update ({"id": 1}, {$push: {"Phone": "123"}});
Where phone is a list, add 123 in. Note: Even if there is 123 in the phone, it will be added, if need not repeat, need to use $addtoset.


5) $addToSet


usage: {$addToSet: {field:value}}


adds a value to the array, and only if the value is not inside the array. Example:
> Db. collection_name. Update ({"id": 1} , {$addToSet: {"Phone": {$each: ["222", "333"]}});


phone can be "123", you can add multiple values, {"$each": ["222", "333"]}, if there is this value, then ignore it.




6) $pushAll

usage: {$pushAll: {Field:value_array}}


with $push, it is possible to append multiple values to an array field at a time. Example:


> Db. collection_name. Update ({"id": 1}, {$pushAll: {"Phone": ["444", "555"]}});


7) $pop


to delete a value within an array


Usage:
Delete last value: {$pop: {field:1}}
Delete First value: {$pop: {field:-1}}


Note that only one value can be deleted, that is, only 1 or-1, not 2 or--and two are deleted.


8) $pull


usage: $pull: {field:value}}


deletes an equal value from the array field. Example:


> Db.collections.update ({"_d": 1}, {$pull: {"Phone": "111"}});


9) $pullAll


usage: {$pullAll: {Field:value_array}}


with $pull, you can delete multiple values within an array at a time. Example:


> DB. collections. Update ({"id": 1}, {$pullAll: {"Phone": ["222", "333"]}})




MongoDB Command Guide

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.