Two ways to run MongoDB
Check if there are Mongodb:which Mongod
Create DATABASE Storage directory: Mkdir-p/data/db
Check the disk directory for space (typically larger than 4G): DF-LH
Start: A direct start: Mongod--dbpath=/data/db--port=27017
B daemon Start: Mongod--dbpath=/data/db--port=27017--fork--syslog//syslog for system logs It's best to use MongoDB's own logs
Best written: Mongod--dbpath=/data/db--port=27017--fork--log=/var/log/mongod.log
Basic use of MongoDB
On the premise that the daemon is turned on
Running MongoDB
-
- MONGO 127.0.0.1:27017
- Mongo
Display database: Show DBS
Create DATABASE: Use MyDB//Note The database is only displayed when the number of data bars is >=1
Insert a piece of data in the Users collection: Db.users.insert ({"Name": "Xiao Wang", "Sex": "Male"})
See all collections: Show Collections
Querying all data from the Users collection: Db.users.find () queries the users collection for data that specifies conditions: Db.user.find ({"Name": "Xiao Wang"})
To view the number of users collection data: Db.users.find (). Count () View the number of data bars for the specified condition Db.user.find ({"Name": "Xiao Wang"}). Count ()
Modify data for the Users collection: db.users.update (condition, modify value, configuration parameter) db.users.update ({"Name:" Xiao Wang "},{$set: {" Age ":" ""}},{multi:true})// Multi refers to whether to modify all the data that meets the criteria, by default modify the first bar
Modify data for the Users collection: Db.users.save ({"_id": ObjectId ("5ae81be4ce517088bda1f195"), "name": "Small Week"})//Note _id is a required field
Delete data from the Users collection: Db.users.remove ({"Name": "Xiao Wang"},true)//true indicates whether to delete a single row, by default deleting all data that meets the criteria
Remove removes the entire document and cannot delete the index
To delete all, use drop
Db.users.drop ()
Summary of the difference between save and update: Save must modify a field must be added to all other fields, and update can specify the field modification, with $set: specified can be
Configuration and use of MongoDB