Document directory
- 2.1 normal unconditional Query
- 2.2.1 single-condition Query
The last short answer described the introduction of MongoDB. This article is about to enter the topic to record mangodb usage.
1. Add data to save
> db.foo.save({"name":"xiaoming"})> db.foo.find(){ "_id" : ObjectId("51e50e633c10628f02cfb779"), "name" : "xiaoming" }
Note the following before using save to add a data entry:
- You do not need to create a set (that is, a data table) in advance. It is automatically created when data is inserted for the first time.
- You can store any data structure without modifying your data structure similar to the alter table statement.
- Each time a new data is inserted, an ID is created in the Set named _ id.
You can add more data points.
> db.foo.save({'name':'xiaoming',age:10,score:90})> db.foo.save({'name':'xiaozhang',age:10,score:85})> db.foo.save({'name':'xiaoli',age:11,score:99})> db.foo.save({'name':'xiaomeng',age:11,score:100})> db.foo.find(){ "_id" : ObjectId("51e50e633c10628f02cfb779"), "name" : "xiaoming" }{ "_id" : ObjectId("51e510133c10628f02cfb77a"), "name" : "xiaoming", "age" : 10, "score" : 90 }{ "_id" : ObjectId("51e510243c10628f02cfb77b"), "name" : "xiaozhang", "age" : 10, "score" : 85 }{ "_id" : ObjectId("51e510353c10628f02cfb77c"), "name" : "xiaoli", "age" : 11, "score" : 99 }{ "_id" : ObjectId("51e510403c10628f02cfb77d"), "name" : "xiaomeng", "age" : 11, "score" : 100 }>
2. query record 2.1 normal unconditional query this operation has been seen above. It is used to query all data in the foo set.
> db.foo.find()
Returned cursor object:
> var cursor=db.foo.find()> while(cursor.hasNext())printjson(cursor.next()){ "_id" : ObjectId("51e50e633c10628f02cfb779"), "name" : "xiaoming" }{ "_id" : ObjectId("51e510133c10628f02cfb77a"), "name" : "xiaoming", "age" : 10, "score" : 90}{ "_id" : ObjectId("51e510243c10628f02cfb77b"), "name" : "xiaozhang", "age" : 10, "score" : 85}{ "_id" : ObjectId("51e510353c10628f02cfb77c"), "name" : "xiaoli", "age" : 11, "score" : 99}{ "_id" : ObjectId("51e510403c10628f02cfb77d"), "name" : "xiaomeng", "age" : 11, "score" : 100}>
The above example shows the iterative output of the cursor style. The hasnext () function tells us whether there is any data. If so, you can call the next () function to obtain data.
MongoDB uses JavaScript shell and can use the JS features. foreach can output the cursor. As follows:
> var cursor=db.foo.find()> while(cursor.hasNext())printjson(cursor.next()){ "_id" : ObjectId("51e50e633c10628f02cfb779"), "name" : "xiaoming" }{ "_id" : ObjectId("51e510133c10628f02cfb77a"), "name" : "xiaoming", "age" : 10, "score" : 90}{ "_id" : ObjectId("51e510243c10628f02cfb77b"), "name" : "xiaozhang", "age" : 10, "score" : 85}{ "_id" : ObjectId("51e510353c10628f02cfb77c"), "name" : "xiaoli", "age" : 11, "score" : 99}{ "_id" : ObjectId("51e510403c10628f02cfb77d"), "name" : "xiaomeng", "age" : 11, "score" : 100}>
It can achieve the same effect as while.
In MongoDB shell, we can use the cursor as data:
> var cursor=db.foo.find();> printjson(cursor[1]){ "_id" : ObjectId("51e510133c10628f02cfb77a"), "name" : "xiaoming", "age" : 10, "score" : 90}>
However, when using the above method, pay attention to the memory usage problem, especially for large cursor objects, which may cause memory overflow. Therefore, we should use the iterative method to output data. The following shows how to convert a cursor to a real array type:
> var arr=db.foo.find().toArray()> arr[2]{ "_id" : ObjectId("51e510243c10628f02cfb77b"), "name" : "xiaozhang", "age" : 10, "score" : 85}
Note that this feature is only used in MongoDB sehll, not all other application drivers. If other users call netx () for the first or last time in the set, you may obtain data without cursor. Therefore, you must explicitly lock the cursor you want to query. 2.2 conditional query 2.2.1 single-condition Query
> db.foo.find({name:'xiaoming'}){ "_id" : ObjectId("51e50e633c10628f02cfb779"), "name" : "xiaoming" }{ "_id" : ObjectId("51e510133c10628f02cfb77a"), "name" : "xiaoming", "age" : 10, "score" : 90 }>
Equivalent to SQL
SELECT * FROM foo WHERE name='xiaoming'
Query the returned data results under a specified condition (similar to a field in the returned table)
> db.foo.find({name:'xiaoming'},{name:true}){ "_id" : ObjectId("51e50e633c10628f02cfb779"), "name" : "xiaoming" }{ "_id" : ObjectId("51e510133c10628f02cfb77a"), "name" : "xiaoming" }
Corresponding SQL statement
SELECT name FROM foo WHERE name='xiaoming'
2.2.2 multi-condition QueryUnfinished, To be continued ......