Addition, deletion, modification and search of mongodb
Added: db.collection_name.insert
Delete: db.collection_name.remove
Update: db.collection_name.update
Query: db.collection_name.find () .pretty ()
Operation example:
1. Create a database
Use test2
two。 Show all current databases
Show dbs
3. The test2 you just created will not be displayed. You need to insert some data into it.
Db.test2.insert ({"name": "I am your uncle"})
two。 Delete database
1. Connect to the database
Use test2
two。 Perform deletion
Db.dropDatabase ()
three。 Create a collection
Use test2
Db.createCollection ("test3")
Show collections
four。 Delete a collection
Use test2
Db.test3.drop ()
five。 New document
Use test2
Db.test3.insert ({title: 'MongoDB tutorial'
Description: 'MongoDB is a Nosql database'
By: 'tutorial'
Url: 'http://www.runoob.com',
Tags: ['mongodb',' database', 'NoSQL']
Likes: 100})
Db.test3.insert ({
Title: 'PHP tutorial'
Description: 'PHP is a powerful server-side scripting language for creating dynamic interactive sites.'
By: 'tutorial'
Url: 'http://www.runoob.com',
Tags: ['php']
Likes: 150})
Db.test3.insert ({
Title: 'TEST tutorial'
Description: 'TEST is a powerful server-side scripting language for creating dynamic interactive sites.'
By: 'tutorial'
Url: 'http://www.runoob.com',
Tags: ['test']
Likes: 200})
Check whether the document has been added successfully:
Db.test3.find () .pretty ()
six。 Update document
Db.test3.update ({'title':'MongoDB tutorial'}, {$set: {'title':'MongoDB'}})
seven。 Delete document
Db.test3.remove ({"title": "TEST tutorial"})
eight。 Query documents, query data in documents with a likelihood value greater than or equal to 150
MongoDB Enterprise > db.test.find ({likes: {$gte: 150}})
{"_ id": ObjectId ("5dd110022ad82fd1af1a82e0"), "title": "PHP tutorial", "description": "PHP is a powerful server-side scripting language for creating dynamic interactive sites." , "by": "tutorial", "url": "http://www.runoob.com"," tags ": [" php "]," likes ": 150}
{"_ id": ObjectId ("5dd110382ad82fd1af1a82e1"), "title": "TEST tutorial", "description": "TEST is a powerful server-side scripting language for creating dynamic interactive sites." , "by": "tutorial", "url": "http://www.runoob.com"," tags ": [" test "]," likes ": 200}