02. Article 2 of MongoDB practice

Source: Internet
Author: User
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 ......
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.