Basic operations
Add and delete to verify a database necessary operations, next we say how to do in the shell under the increase and deletion check.
We also open a "command indicator" window (note: cannot be closed before), enter MONGO open MongoDB shell, this shell is the client of MongoDB
F:\mongodb\data\bin>mongoMongoDB shell version: 3.4.4connecting to: test>
The default connected database is "test"
Increased operation
Let's add a collection called "Person" (which can be understood as a database table)
> db.Person.insert({"name":"张三", "age":10})WriteResult({ "nInserted" : 1 })> db.Person.insert({"name":"李四", "age":20})WriteResult({ "nInserted" : 1 })>
Query operations
Conditional query:
> db.Person.find({"name":"张三"}){ "_id" : ObjectId("5617275737a5aa2cafdb4b84"), "name" : "张三", "age" : 10 }>
Query all:
> db.Person.find(){ "_id" : ObjectId("5617277b37a5aa2cafdb4b85"), "name" : "李四", "age" : 20 }{ "_id" : ObjectId("56172d069d1af21ad1f769c7"), "name" : "张三", "age" : 40 }>
Note : Did you find out? The collection of a "**_id**" field, this field is the default to join the GUID, the purpose is to ensure the uniqueness of the data, in the relational database, we will add a primary key to the table, in MongoDB can save themselves and the primary key.
Update action
The update command has two parameters, the first one is "condition found" and the second is "updated value"
> DB.Person.update ({"Name": "Zhang San"}, { "name": "Zhang San", " age ": 40}) writeresult ({ "nmatched": 1, "nUpserted": 0, "nmodified": 1}) > Db.person. find ({ "name": "Zhang San"}) { "_id": objectid ( "56172d069d1af21ad1f769c7"), Span class= "hljs-string" > "name": "Zhang San", "age": 40}>
Delete operation
> db.Person.remove({"name":"张三"})WriteResult({ "nRemoved" : 1 })> db.Person.find(){ "_id" : ObjectId("5617277b37a5aa2cafdb4b85"), "name" : "李四", "age" : 20 }
MongoDB Learning II