MongoDB Study Notes

Source: Internet
Author: User
Tags mongodb client mongodb commands mongodb query mongodb sql

Basic concepts:

1. Document --> corresponds to the row of the relational database, that is, a record. It is more powerful than the row functions of relational databases, and more like a specific object. The document is displayed in the form of a Map. Of course, the value can be of any type, or it can continue to be a document (recursive definition)

2. Set --> tables corresponding to the relational database. However, it is stateless, that is, the document does not need to be consistent.

Basic operations:

1. show dbs

2. Create a database: use yourDateBaseName to create a database. However, if you use show dbs, you cannot display yourDataBaseName because the database has no operations. So if you leave, the database will be abolished. If you insert a record, you can see the corresponding database through show dbs as follows: record = {"name": "wang", "age": 20, "password ": "123456"} db. yiyou. you_upload_photo.insert (record): use show dbs to view your database.

3. display the database show collections. Default include system. indexes table

4. Create a set: because the integration in mongodb is stateless, unlike traditional relational databases, it is necessary to declare in advance what fields the table contains and define the attributes of fields. Although there is no need to pre-define. Insert directly when using it. See the preceding example.

5. Delete the set db. yourDatabaseName. yourCollectionName. drop ()

6. Insert the document into the set record = {"name": "wang", "age": 20, "password": "123456"} db. yourDatabaseName. yourCollectionName. insert (record)

7. Deleting the document db. yourDatabaseName and yourCollectionName. remove () in the set will clear all documents in the set.

8. There are two types of updates in Mongo: replace the old one with the new one. [This is to select a document that meets the conditions and replace the old one after modification.] I do not like this operation, which violates atomicity. In addition, the Mongo modifier is used to modify the document. Modifier list:

8.1. "$ set" modifier: Specifies a healthy value. If Jian does not exist, create it. Example: db. myMongodb. user. update ({"_ id": ObjectId ("4ed373c46d375f1a1960ed07") },{ "$ set": {"sex": "male "}}) "$ unset" deletes a Jian Example: db. myMongodb. user. update ({"_ id": ObjectId ("4ed373c46d375f1a1960ed07") },{ "$ unset": {"sex": 1 }})

8.2 The "$ inc" modifier adds an existing key value or creates a key when the key does not exist. Example: db. myMongodb. user. update ({"_ id": ObjectId ("4ed373c46d375f1a1960ed07") },{ "$ inc": {"age": 1}) Tips: $ inc can only be used for integers, long Integer and double-precision floating point number. Other data may fail.

8.3 "$ push" adds an element to the end of an existing array. If it does not exist, a new array is created. Example: db. myMongodb. user. update ({"_ id": ObjectId ("4ed373c46d375f1a1960ed07") },{$ push: {"comments": "this is my comments "}})

8.4 "$ ne" not equal judge, [Determining the given attribute is not equal to the given value] Example: db. myMongodb. user. update ({"comments": {"$ ne": "this is comments" },{ $ push: {"comments": "your comments "}})

8.5 add Example: db without repeating "$ addToSet. myMongodb. user. update ({"_ id": ObjectId ("4ed373c46d375f1a1960ed07") },{ "$ addToSet": {"comments": "your comments "}})

8.6. "$ each" array traversal modifier Example: db. myMongodb. user. update ({"_ id": ObjectId ("4ed373c46d375f1a1960ed07") },{ "$ addToSet": {"comments": {"$ each": ["a commnet ", "B comment"] }}})

8.7. The "$ pop" modifier deletes the element Example: {$ pop: {key: 1} from any end of the array and deletes an element {$ pop: {key:-1} deleted from the array Header

8.8. array location modifier Example: db. myMongodb. user. update ({"comments. author ":" john "},{" $ set ": {" comments. $. author ":" jim "}) locate the first comment of john.

9. A special update or insertion of upsertUpsert is characterized by an update if it exists. If it does not exist, a new record is inserted according to the update condition.

10. For batch update, set the fourth parameter of update to true. Otherwise, the first matching document is updated by default.

11. Obtain the execution result db. runCommand ({getLastError: 1}) of the previous command })
Mongodb query operations

1. query all sets of db. myMongodb. post. find (). If it is a console, 20 records are displayed by default.

2. Return the specified key db. myMongodb. post. find ({}, {"foo": 1, "baz": 1}) Return key foo and baz, And the _ id key value is returned by default.

3. Specify the key db. myMongodb. post. find ({}, {"foo": 0}) not returned to return keys other than foo.

4. query criteria a) "$ lt" is less than B) "$ lte" is less than or equal to c) "$ gt" is greater than d) "$ gte" is greater than or equal to e) "$ ne" is not equal to db. myMongodb. user. find ({"age": {"$ gte": 18, "$ lte": 30 }})

5. other condition queries a) "$ in": whether to query the database in a set. myMongodb. user. find ({"age": {"$ in": [17,18, 19]}) B) "$ nin": corresponds to in, not in a combined document c) "$ or": Multi-condition query db. myMongodb. user. find ({"$ or": [{"age": 17 },{ "age": 18}]}) d) "$ not" is not used. Other conditions can be based on it e) "$ mod" modulo query f) "$ null" can match a field with a null value, it can also match a document with a key that does not exist. If you want to determine whether a key exists at the same time, you need to add "$ exists ".

6. query array a) "$ all" matches the array db with multiple elements. myMongodb. food. find ({"fruit": {"$ all": ["apple", "oracle"]}) B) array subscript matches db. myMongodb. food. find ({"fruit.2": "peach"}) c) "$ size" array length matching db. myMongodb. food. find ("fruit": {"$ size": 3 })

7. "$ slice" specifies the number of db returned from the set. myMongodb. posts. find (criteria, {"$ silce": n}) n = 10 returns the first 10 records n =-10 returns the last 10 records n = [23, 10] Return the 10 records starting from 23 records. The page may be displayed.

8. query embedded documents (recommended) db. myMongodb. find ({"name. first": "Wang", "name. last": "Hai"}) using dot notation "})

9. Hierarchical query of db. myMongodb. find ({"name": {"first": "Wang," last ":" Hai ""}})

10. $ where query is very powerful. It can add js functions to implement advanced query functions. The basic style is as follows: db. myMongodb. find ({"$ where": function () {}}) If function returns true, this record is returned. Otherwise, no response is returned.

Reference recommendations:

Common mongodb commands

MongoDB SQL syntax comparison

MongoDB client terminal vue

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.