MongoDB Tips and Considerations Summary

Source: Internet
Author: User
Tags mongodb add mongo shell
Mongodb, a distributed document storage database, written in the C + + language, is designed to provide scalable, high-performance data storage solutions for Web applications. MongoDB is a high-performance, open-source, modeless document-based database that is a popular one in the current NoSQL database. This article will share with you the problem solving skills that you encounter when using MongoDB, and share it for your reference.

One, find the record that the array field is not empty

Finds records in which the array field in the data is not empty.

For example: There are the following MONGO documents,

{"id": "581c060f2b436c05aafb1632", "commit_history": [  "581c20d52b436c05aafb1633",  " 581c21c12b436c05aafb1634 "]},{" id ":" 581c060f2b436c05aafb1633 "," commit_history ": []}

To find records for which Commit_history is not empty, the following methods are available:

Method One: Db.collection.find ({commit_history: {$not: {$size: 0}})

Method Two: Db.collection.find ({' commit_history.0 ': {$exists: 1}})

Second, MongoDB add users

To add a user to a collection in MongoDB, you can do the following:

Use collection_name switch to a library

Db.createuser ({User: "collection_name", pwd: "Password", roles: ["ReadWrite", "DbAdmin"]})

Third, sometimes you need to delete the column of the specified field, using the update operation.

For example, to remove the column name:

Query JSON:

{"Name": {$exists: true}}

Update JSON:

{$unset: {"name": ""}}

Iv. data export, execute the mongoexport command in the MongoDB Bin directory and set the relevant parameters

For example:

./mongoexport-h 192.168.0.201--port 27017–d admin–u admin–p admin-c department-o/home/admin/department.dat

-H: Specifies the IP of the database to connect to;

--port: Specifies the port of the database to which you want to connect;

-U: Specifies the user name of the database to connect to;

-P: Specifies the user password of the database to connect to;

-D: Specifies the name of the library to connect to;

-C: Specifies the collection of data to be exported;

-O: Specifies the address of the data destination to be exported;

Note: (1), need to ensure that the connected database is in a normal state of operation;

(2), I have encountered a situation, the database has been added to the user information, the startup is not user authentication to start, but the execution of this command, or when I specify the user name and password in the case of export success, if someone encountered similar situation, may wish to try.

V. Data import, execute the mongoimport command in the MongoDB Bin directory and set the relevant parameters, parameter interpretation and the same as above

For example:

./mongoimport--port 27017-d admin-u admin–p admin–c department/home/common/mongodb305/bin/department.dat


Vi. user authentication issues for non-Amdin databases:

We add users to the libraries in the MongoDB database and can use the following commands in the target database, such as adding a user with Read and write permissions to the Mongotest library:

Db.createuser ({"User": "Test", "pwd": "123456", "Roles": ["ReadWrite"]})


You can also add the following in the admin database:

Db.createuser ({"User": "Test", "pwd": "123456", "roles": [{"Role": "ReadWrite", "DB": "Test"}, "ReadWrite"]})


It is important to note that there is a difference between the two ways, and it is precisely this distinction that once pits me:

Using the first way to add, we can directly execute the following command in the bin directory of MongoDB to enter the test database operation, adding or removing changes can be, you can also use this user name and password in the Mongovue connection:

./mongo-h 192.168.0.201--port 27017-u test-p 123456-d test


However, if the second way to create, then directly using the command above, will prompt the verification failure, only yourselves into the MONGO shell connection to the Admin database, and then switch to the test database to verify the pass. This is a small pit, the unknown situation may be very tangled, clearly user name and password is not a problem, but I do not know why is not even.

Seven, mongodb3.0 default data storage mode is the same as the original 2.6, I tried to change to a new storage mode, the use of the following parameters at startup, but it is necessary to note that there is no data in the database before the premise, or will be error:

./mongod-f/mongodb304/conf/mongodb.conf--storageengine Wiredtiger

Various other parameters, such as DBPath, LogPath, etc., are configured in the mongodb.conf.

The above content is MongoDB daily use of skills and attention to the summary, hope to help everyone.

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.