Basic mongoDB shell operations

Source: Internet
Author: User
Tags findone
To open the shell, first start mongod -- dbpathd: app1_data on the server, and then open shell: mongolocalhost: 27017. The default value is test user. We can increase the permission by using mongolocalhost: 27017admin. create a database use [databaseName], but exit the database if nothing is done.

To open the shell, first start mongod -- dbpath d: \ app \ mongodata on the server, and then open shell: mongo localhost: 27017, which is operated by the test user by default. We can use mongo localhost: 27017/admin increase permissions 1. create a database use [databaseName], but exit the database if nothing is done.

Open shell

Start mongod -- dbpath d: \ app \ mongodata on the server first.

Open shell: mongo localhost: 27017

By default, the test user operation is performed. You can use mongo localhost: 27017/admin to increase the permission.

1. Create a database

Use [databaseName]

However, if nothing is done, the empty database will be deleted.

2. view all databases

Show dbs

3. Add a set to the specified database and add records

Db. [documentName]. insert ({...})

1. Insert document

Db. [documentName]. insert ({})

2. Insert documents in batches

The execution of shell is incorrect db. [documentName]. insert ([{},{},{}, ......]).

Shell does not support batch insert

To complete batch insertion, you can use the mongo application driver or the shell for loop.

3. Save operation

The difference between the save operation and the insert operation is that when _ id is the same

Save

Insert will report an error

4. view all documents in the database

Show collections

5. query document data

Query all: db. [documentName]. find ()

Query the first data: db. [documentName]. findOne ()

6. Update document data

Db. [documentName]. update ({query condition}, {update content })

Example:

Var p = db. persons. findOne ();

Db. persons. update (p, {name: "zhang "});

1. Tough Document Replacement update operations

Db. [documentName]. update ({queryer}, {modifier })

Tough updates will replace old documents with new documents

2. When a primary key conflict occurs, an error is reported and the update operation is stopped.

This is a tough replacement. When the replaced document conflicts with the existing document ID, the system reports an error.

3. insertOrUpdate operation

Purpose: The queryer will update the data and replace the data if the data is not found.

Practice: db. [documentName]. update ({queryer}, {modifier}, true)

4. Batch update operations

By default, the first data entry is modified when the queryer queries multiple data entries.

How to Implement batch Modification

Db. [documentName]. update ({queryer}, {modifier}, false, true)

7. Delete the data in the document

Db. [documentName]. remove ({...})

Example:

Db. persons. remove ({name: "zhang "})

1. Delete all data in the list

Db. [documentName]. remove ()

The set itself and the index will not be deleted

2. Delete based on conditions

Db. [documentName]. remove ({})

Delete records in the set text with the name equal to uspcat

Db. text. remove ({name: "uspcat "})

3. Tips

If you want to know a collection with a huge amount of data

Directly Delete the set and re-create the index

It is much more efficient than simply using remove.

8. Use the modifier to complete partial update operations

. RunCommand and findAndModify Functions

RunCommand can execute special functions in mongoDB.

FindAndModify is one of the special functions. It is used to return the document after update or remove.

RunCommand ({"findAndModify": "processes ",

Query: {queryer },

Sort {sort },

New: true

Update: {Updater },

Remove: true

}). Value

Ps = db. runCommand ({

"FindAndModify": "persons ",

"Query": {"name": "text "},

"Update": {"$ set": {"email": "1221 "}},

"New": true

}). Value

Ps)

9. $ addToSet and $ each are combined to complete batch array update

Db. text. update ({_ id: 1000 },{$ addToSet: {books :{$ each: ["JS", "DB"] }}})

$ Each loops through the following array to perform the $ addToSet operation on each value.

10. Allocation and query efficiency

When a document is created, the DB allocates memory and reserved memory for it. When the document is modified

When the reserved inner layer is not exceeded, the speed is very fast. If the reserved inner layer is exceeded, a new memory will be allocated.

It will consume time

11. Delete the set db. [documentName]. drop () in the database ()

Delete database db. dropDatabase ()

12. help in shell

Global help, database-related db. help (), set-related db. [documentName]. help ()

Note: The shell client has a built-in js engine!

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.