Getting started with mongodb
I wrote how to install: http://blog.csdn.net/stronglyh/article/details/46805373
This article introduces mongodb
First, check whether the service has been opened. There are two ways to open the service. The first is to use the doscommand: net start MongoDB (provided that the service name we named when installing mongodb is MongoDB ), the second method is to open the control panel-> Administrative Tools-> service and find the MongoDB service to check whether it is enabled.
After the installation is complete, enter the doscommand dialog box and go to the bin folder of the mongodb installation directory.
At this time, we may wonder if there will be several databases in oracle: show database, which is different in mongodb. The command in mongodb is show dbs.
Next we will create a new database. Generally, we can create a database using the use method.
Oh, did it happen?
1: insert
Db. users. insert ({name: "hhh", age: 26 })
2: Batch insert
Db. users. insert ([{name: "tommy"}, {name: "xiaoming"}])
3: Query documents
Db. users. find ();
3.1 equal conditions
Db. users. find ({age: 26 })
3.2 comparison Conditions
Greater than ($ gt), greater than or equal to ($ gte), less than ($ lt), less than or equal to ($ lte), not equal to ($ ne), contained in ($ in), not included in ($ nin ).
For example, db. user. find ({age: {$ lt: 30 }})
3.3 Sub-Files
Db. mycol. find ({"access. level": 5 });
3.4 array Conditions
Array exact match:
Db. inventory. find ({tags: ['fruit', 'food', 'citrus ']});
Match a single element:
Db. inventory. find ({tags: 'fruit '});
Match specific elements:
Db. inventory. find ({'tags. 0': 'fruit '});
3.5 compound Query
Query all documents whose type field is food and the price is lower than 95 in the inventory set.
Db. inventory. find ({$ and: [{type: "food" },{ price :{$ lt: 95}]});
All data with a retrieval quantity (qty) greater than 100 or a ($ or) price less than 9.95 in the inventory set.
Db. inventory. find ({
$ Or :[
{Qty :{$ gt: 100 }},
{Price :{$ lt: 9.95 }}
]
});
3.6 restrictions
Limit
Db. users. find (). limit (3) first three data items
3.7 Expected result set
The traditional method is to write all the actual fields in the result set. If you only want to use the actual fields -------------
Db. collection. find (query condition, font condition)
Parameters:
Field Condition: JSON object, format => {field: Value}. If the value is equal to 1, return is required. If the value is 0, no return is required.
Example db. users. find ({}, {status: 1, age: 1}
4. Update the document
Db. collection. update (query, update, {upsert: boolean, multi: boolean });
Parameters:
Query: query condition. This document is the same as the query Condition Statement in "find.
Update: modify content, document.
Upsert (optional): If the value is true, a document is created when no matching document exists in the set. The default value is false.
Multi (optional): If the value is true, all eligible documents will be updated. Otherwise, only one document will be updated. The default value is false.
Example:
Modify the status of A person over 19 years old to
Db. users. update (
{Age :{$ gt: 19 }},
{$ Set: {status: ""}})
Query: Documents older than 19 years: db. users. find ({age: {$ gt: 19 }})
We found the updated sentence.
5: operate on a single document. Run the save: db. collection. save () command ();
Db. users. save ({name: 'hangsan', age: 25, sex: true });
6. Delete
For example, if the deletion status is
Db. users. remove ({status: ""})
First, check whether it exists.
Db. users. find ({status: ""})
Then delete the statement. After execution, execute the query statement and find that the statement has been deleted.
6: cursor
The find command does not directly return results, but returns an iterator of the result set, that is, a cursor.
To obtain data, we can use the next method to traverse the cursor, as shown below:
Var myCursor = db. inventory. find ({type: "food "});
Var myDocument = myCursor. hasNext ()? MyCursor. next (): null;
If (myDocument ){
Var myItem = myDocument. item;
Print (tojson (myItem ));
}
You can use the forEach method to traverse the cursor as follows:
Var myCursor = db. inventory. find ({type: "food "});
MyCursor. forEach (printjson );
The next article is about mongodb advanced. Stay tuned
Zookeeper
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.