Mongodb command guide and mongodb Guide

Source: Internet
Author: User
Tags mongodb find mongodb update

Mongodb command guide and mongodb Guide

Welcome to reprint, reprint please indicate the original address: http://blog.csdn.net/majianfei1023/article/details/45166827


1. What is MongoDB?

In short, MongoDB is a type of database, such as mysql, SQL Server, and orcale. however, unlike these databases, they are all relational databases, while MongoDB is non-relational databases (NoSql ). MongoDB is a high-performance, open-source, and non-pattern document-based database. It is a popular NoSql database. It can be used in many scenarios to replace traditional relational databases or key/value storage methods. Mongo is developed using C ++. Mongo's official website address is http://www.mongodb.org/. the reader can provide more detailed information here.

MongoDB has three basic terms: database, set, and document. Everyone knows what the database is. The set is equivalent to a mysql table, and the document is equivalent to a column in the table.

That's all right.

First, let's talk about the database and set commands:

1. view all databases:

Show dbs

2. Use a database

Use mydb

3. view all the collections in this database:

Show collections


Ii. Query: MongoDB find


Db. collection. find (query, fields)
Query is optional. Use the query operator to specify query conditions.
Fields (optional) specifies the return key. All key values in the document are returned during query. You only need to omit this parameter (omitted by default)


1) query all documents in the set.


// All documents in the collection will be returned
Db. collection. find ()
// Or
Db. collection. find ({})


If the first parameter is a key/value pair, conditional filtering (query) is executed during the query. The following query operation returns a document set with the age key value of 18 in the collection.


Db. collection. find ({"age": 18 })
// You can also query multiple conditions by commas (,).
Db. collection. find ({"age": 18, "sex": "man "})


We can use the second parameter of find to specify the return key.


If find does not specify the second parameter, the query operation returns all the key values in the query document by default. The returned keys can also be specified in mongo, so that we can avoid the resources consumed by querying useless key values, saving the amount of data transmitted and memory consumption.


Db. users. find ({}, {"age": 1 })
Note that {"age": 1} does not mean age = 1, because it is in the second parameter (fields), it refers to finding all the age in the document, of course, you can also set multiple values.




If the data is double-layered. For example:
"User ":{
"Name": "mm ",
"Age": 10
}


Both query and fields are supported. For example, you need to query all players whose age is 10.
Db. collection. find ({"user. age": 18 })


The query conditions are not only equal, but can also be compared with other operations.
For example, we use the following comparison operators "$ gt", "$ gte", "$ lt", "$ lte ", "$ ne" (corresponding to ">", "> =", "<", "<= ","! =.
For example, you can query players older than 20 and less than 30 years old.
Db. collection. find ({"age": {"$ gt": 20, "lt": 30 }})


$ In


Find the document of the key in the values list.


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


Mongodb is flexible. All the above queries can be combined at will, or used for update. Let's start your imagination as long as it meets the mongodb syntax.






Iii. update: MongoDB update
Mongodb update uses the update document.


Db. collection. update (criteria, objNew, upsert, multi)


Query: the query condition of the update statement, similar to
Fields: the update object and some updated operators (such as $, $ inc...) can also be understood as
Upsert: this parameter indicates whether to insert objNew if no update record exists in the document. true indicates insertion. The default value is false and no insertion is performed.
Multi: the default value of mongodb is false. Only the first record found is updated. If this parameter is set to true, all the records identified by the condition are updated.



Upsert and multi are simple. The following describes query and fields:


1) $ set
Usage: {$ set: {field: value }}
It is equivalent to SQL's set field = value. All data types support $ set.


Db. collection_name.update ({"id": 1 },{$ set: {"name": "mm "}})
Set is to set the value of key to name to "mm"


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

Add value to a numeric field, for example:


Db. collection_name.update ({"id": 1 },{$ set: {"number": 1 }})
As shown above, if there is no name or number but you need to add (or even the doc with this "id" = 1), you need to set upsert to true.


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


As the name suggests, fields are deleted. Example:
> Db. collection_name.update ({"id": 1 },{$ unset: {"name": 1 }})
Delete the name field of the doc.

I don't know what field: 1 is.


Array Operation:


4) $ push


Usage: {$ push: {field: value }}


Append the value to the field. The field must be of the array type. If the field does not exist, an array type is added. Example:


> Db. collection_name.update ({"id": 1 },{$ push: {"phone": "123 "}});
Phone is a list and 123 is added. Note: even if the phone contains 123, it will still be added. If you do not need to repeat it, you need to use $ addToSet.


5) $ addToSet


Usage: {$ addToSet: {field: value }}


Add a value to the array. This value is added only when it is not in the array. Example:
> Db. collection_name.update ({"id": 1 },{$ addToSet: {"phone" :{$ each: ["222", "333"] }});


Phone can be followed by "123" or multiple values, {"$ each": ["222", "333"]}. If this value exists, ignore it.




6) $ pushAll

Usage: {$ pushAll: {field: value_array }}


The same as $ push, but multiple values can be appended to an array field at a time. Example:


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


7) $ pop


Deletes a value from the array.


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


Note that only one value can be deleted, that is, only 1 or-1 can be used, but 2 or-2 cannot be used to delete two values.


8) $ pull


Usage: $ pull: {field: value }}


Deletes a value equal to the value from the array field. Example:


> Db. collections. update ({"_ d": 1 },{$ pull: {"phone": "111 "}});


9) $ pullAll


Usage: {$ pullAll: {field: value_array }}


Same as $ pull, multiple values in the array can be deleted at a time. Example:


> Db. collections. update ({"id": 1}, {$ pullAll: {"phone": ["222", "333"]})




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.