What is a MongoDB shell?
The Mongdb shell is a fully functional JavaScript interpreter that can run JavaScript programs.
However, the role of the shell is much more than this, it is mainly used to connect the MongoDB server, execute the script, the database operation. Administrative tools like SQL Server SSMs.
The shell console can be opened with the following 2 commands:
> CD C:\Program files\mongodb 2.6 standard\bin> MONGO
:
C:\Program FILES\MONGODB 2.6 Standard is the installation directory for MongoDB.
Before opening the shell console, make sure that the MongoDB service is started, and how to start MongoDB can refer to MongoDB Primer one: Installation and configuration.
MongoDB Shell Basic operations 1. Add
> Db.blog.insert ({"title": "AAA", "Content": "123", "date": New Date ()})
Adding a piece of data using the Insert method, MongoDB implicitly creates the collection blog. If you add a document that is more complex, you can declare it before adding it, for example:
> post = {"title": "AAA", "content": "123", "author": "Ming Li", "Summary": "This article focuses on ASP. ", " date ": new Date ()}> Db.blog.insert (POST)
2. Enquiry
The Find method without parameters returns all of the documents in the collection, and the shell displays only 20 bars. If you want to see only one piece of data, you can use the FindOne method, and FindOne with no parameters will return the first data in the current collection.
> Db.blog.find () > Db.blog.findOne ()
If you want to query the collection blog for the title "AAA" document, you can add parameters to find:
> Db.blog.find ({"title": "AAA"})
If you can only make such a query estimate a lot of people will be crazy, in fact, MongoDB query is very powerful, need to be introduced separately, here only need to have a preliminary understanding.
3. Modifications
Modify using the Update method, update is a method with at least 2 parameters.
If the collection book has the following documents:
{"_id": One, "Item": "Chang-An Chaos", "publisher": "Spring Breeze Literary Press", "author": "Han Han", "Price": 20}
To modify the price of the book to 19, you can use the following command:
> db.book.update ({"_id": 11},{"price": 19})
What if you want to add properties to it? If I want to increase the price by 10 and add a stock property with a value of 5, you can use the following command:
> db.book.update ( {"_id": one}, { $.set:{stock:5}, $.inc:{price:10} } )
This is the case with the modified document:
{"_id": One, "Item": "Chang-An Chaos", "publisher": "Spring Breeze Literary Press", "author": "Han Han", "Price": "Stock": 5}
There are a lot of things to say about the changes, this article mainly introduces the basic operation of MongoDB, I will detail the Update method in the following article.
4. Delete
Remove is used to permanently delete documents from the database. Without using any parameters, it deletes all documents within the collection. It can also accept parameters of a document type to qualify the condition.
For example, the following command deletes a document with the title AAA in the blog:
> Db.blog.remove ({"title": "AAA"})
Other common commands for the shell
1. View all commands or methods under DB
2. Switch to database test, if test does not exist, it will be created automatically.
3. Delete the currently connected database
> db.dropdatabase ()//please operate with caution
4. Querying all Local database names
> Show DBS
5. Querying all collections under the current database
Show collections
6. Query the number of documents under the current collection
Db.collection.count ()//Example: Db.blog.count ()