Mongodb, a distributed document storage database, written in C++ language, aims to provide scalable, high-performance data storage solutions for WEB applications. MongoDB is a high-performance, open source, schema-less document database, which is currently a popular NoSQL database. This article will share with you the solving techniques for problems encountered when using MongoDB, and share them for your reference and learning.
1. Find records whose array fields are not empty
Find records whose array fields are not empty in the data.
For example: There is the following Mongo document,
{ "id" : "581c060f2b436c05aafb1632", "commit_history" : [ "581c20d52b436c05aafb1633", "581c21c12b436c05aafb1634" ] }, { "id" : "581c060f2b436c05aafb1633", "commit_history" : [] }
If you want to find records whose commit_history is not empty, there are the following methods:
Method 1: db.collection.find ({commit_history: {$not: {$size: 0}}})
Method 2: db.collection.find({'commit_history.0': {$exists: 1}})
2. Add a user to MongoDB
To add a user to a Collection in MongoDB, you can do the following:
use collection_name to switch to a library
db.createUser( { user: "collection_name", pwd: "password", roles: [ "readWrite", "dbAdmin" ] } )
3. Sometimes it is necessary to delete the column of the specified field and use the update operation.
For example, to delete the name column:
query json:
{"name":{$exists:true}}
update json:
{$unset:{"name":""}}
4. Data export, in mongodb Execute the mongoexport command in the 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: Specify the IP address of the database to be connected;
--port: Specify the database to be connected The port of the database;
-u: Specify the user name of the database to be connected;
-p: Specify the user password of the database to be connected;
-d: Specify the library name to be connected;
-c: Specify the data collection to be exported;
-o: Specify the target storage address of the data to be exported;
Note: (1) It is necessary to ensure that the connected database is in normal operation;
(2) I once encountered a situation where user information was added to the database and it was not started with user authentication when starting. However, when executing this command, it was only after I specified the user name and password. The export was successful. If anyone 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 parameter explanation is the same as above
For example:
./mongoimport --port 27017 -d admin -u admin –p admin –c department /home/common/mongodb305/bin/department.dat
6. User verification issues for non-amdin databases:
We add users to the library in the mongodb database. You can use the following commands in the target database, for example, add a user in the mongoTest library. Users with read and write permissions:
db.createUser({"user":"test","pwd":"123456","roles":["readWrite"]})
can also be added in the admin database:
db.createUser({"user":"test","pwd":"123456","roles":[{"role":"readWrite","db":"test"},"readWrite"]})
It should be noted that : There is a difference between these two methods, and it is this difference that once fooled me:
When using the first method to add, we can directly execute the following command in the bin directory of mongodb to enter. You can operate in the test database, add, delete, modify or check; you can also use this user The name and password are connected 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 directly using the above command, it will prompt that the verification failed. Only when entering mongo first The shell is connected to the admin database, and then the verification can be passed when switching to the test database. This is a small pitfall. Those who don’t know the situation may be very confused. It is clear that the username and password are fine, but they don’t know why they can’t connect.
7. The default data storage method of mongodb3.0 is still the same as that of mongodb 2.6. I have tried changing to the new storage method and using the following parameters at startup. However, it should be noted that there needs to be no data in the database. This is only possible if the data is required, otherwise an error will be reported:
./mongod -f /mongodb304/conf/mongodb.conf --storageEngine wiredTiger
mongodb.conf is configured with various other parameters for startup, such as dbpath, logpath, etc.
The above content is a summary of the tips and precautions for daily use of MongoDB. I hope it can help everyone.
Related recommendations:
How does phpstudy extend MongoDB
How to improve the security of MongoDB
The above is the detailed content of Summary of MongoDB tips and precautions. For more information, please follow other related articles on the PHP Chinese website!