Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the skills and points for attention in the daily use of MongoDB

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article is to share with you about the daily use of MongoDB tips and notes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Find records whose array fields are not empty

Find records in the data whose array fields are not empty.

For example: there are the following Mongo documents

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

To find records whose commit_history is not empty, there are the following ways:

Method 1: db.collection.find ({commit_history: {$not: {$size: 0})

Method 2: db.collection.find ({'commit_history.0': {$exists: 1}})

2. Add users to MongoDB

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

Use collection_name switches 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 and use the update operation.

For example, to delete the column name:

Query json:

{"name": {$exists:true}}

Update json:

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

Fourth, data export, execute the mongoexport command in the bin directory of mongodb 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: specify the ip of the database to connect to

-- port: specify the port of the database to connect to

-u: specify the user name of the database to connect to

-p: specify the user password of the database to connect to

-d: specify the name of the library to connect to

-c: specify the data set to export

-o: specify the storage address of the data destination to be exported

Note: (1) need to ensure that the connected database is in a normal running state

(2) I have encountered a situation in which user information was added to the database and started without user authentication, but when executing this command, it was successfully exported only if I specified a user name and password. If someone encounters a similar situation, you might as well give it a try.

5. Data import. Execute the mongoimport command in the bin directory of mongodb and set the relevant parameters. The parameters are interpreted as above.

For example:

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

6. User authentication of non-amdin databases:

To add a user to the library in the mongodb database, you can use the following command 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 to 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 this difference that has fooled me:

When using the first method, we can directly execute the following command under the bin directory of mongodb to enter the test database. You can add, delete, change or query it. You can also use this user name and password to connect in mongoVUE:

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

But if it is created in the second way, then using the above command directly will indicate that the verification failed. It can only be verified when you enter mongo shell to connect to the admin database and then switch to the test database. This is a small hole, unknown situation may be very tangled, obviously the user name and password are fine, but do not know why it is not connected.

7. The default data storage method of mongodb3.0 is the same as the original 2.6. I have tried to change it to a new storage method, using the following parameters when starting, but it should be noted that it can only be done if there is no data in the database, otherwise an error will be reported:

. / mongod-f / mongodb304/conf/mongodb.conf-- storageEngine wiredTiger

Various other startup parameters are configured in mongodb.conf, such as dbpath, logpath, and so on.

Thank you for reading! This is the end of this article on "what are the skills and precautions for the daily use of MongoDB?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report