MongoDB Learning Notes (ii)

Source: Internet
Author: User
Tags findone

One, MongoDB command
说明:Mongodb命令是区分大小写的,使用的命名规则是驼峰命名法。  对于database和collection无需主动创建,在插入数据时,如果database和collection不存在则会自动创建。
Common commands

Help command
With this command you can see some of the most basic commands,

Use command
For example, command "Use Demodb", create demodb without worrying that DEMODB will not be created, and automatically create a database collection when using the use DEMODB command to create the first Demodb.

Inserting data
Insert a document using the command "Db.collectionName.insert ({name:" Jack ", age:33})" CollectionName and create if CollectionName does not exist. Use the command "Db.getcollectionnames ()" To get CollectionName and System.indexex. System.indexex is available for each database and is used to record index. Use the command "Db.collectionName.find ()" To view all the document in CollectionName. The command is as follows:

E:\mongodb\bin>mongomongodb Shell version:2.6.5connecting to:test> use demodbswitched to DB demodb> db. Firstcollection.insert ({name: "Jack", age:22}) Writeresult ({"ninserted": 1}) > Show Collectionsfirstcollectionsystem.indexes   > Db.getcollectionnames () ["Firstcollection", "system.indexes"] > Db.demodb.find () > db. Firstcollection.find () {"_id": ObjectId ("543731431dc491f307663a0d"), "name": "Jack", "Age":}> db.system.indexes . Find () {"V": 1, "key": {"_id": 1}, "name": "_id_", "ns": "Demodb." Firstcollection "}

Querying data

Reference URL: http://www.cnblogs.com/stephen-liu74/archive/2012/08/03/2553803.html

MongoDB使用find来进行查询.查询就是返回一个集合中文档的子集,子集合的范围从0个文档到整个集合.  find的第一个参数决定了要返回哪些文档(document的过滤条件).其形式也是一个文档,说明要查询的细节.空的查询文档{}会匹配集合的全部内容.  要是不指定查询文档,默认是{}.如:db.users.find()返回集合中的所有内容.  向查询文档中添加键值对,就意味着添加了查询条件.对绝大多数类型来说,整数匹配整  数,布尔类型匹配布尔类型,字符串匹配字符串.

Add test Data First

Db. Student.insert ({name: "Jack", sex:1,age:33}) db. Student.insert ({name: "Jack", sex:1,age:33}) db. Student.insert ({name: "Lily", sex:0,age:13}) db. Student.insert ({name: "kaily", sex:0,age:33}) db. Student.insert ({name: "Tom", sex:1,age:53})

1. Find ()/findone () conditional filtering

Only get name equals Jack's student. FindOne () only gets the first bar

> Use demodbswitched to DB demodb>  db.  Student.find ({name: "Jack"}) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Sex": 1, "age": {"_id" : ObjectId ("543738B857ABAFE09D99CBFD"), "name": "Jack", "Sex": 1, "age":}>  db. Student.findone ({name: "Jack"}) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Sex": 1, "Age": 33}

2, find ()/findone () specifies the returned fileds

说明:find()的第二个参数限制返回的filed的个数,0代表不返回,1代表返回。"_id"键总是会被返回。  如果不带条件,只限制返回的filed个数的话,命令如下:db.Student.find({},{sex:0})。只需要第一个参数为{}空字典就可以。

Only get name equals Jack's student, and filed is Name,age data.

> DB. Student.find ({name: "Jack"},{name:1,age:1}) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Age": 33} { "_id": ObjectId ("543738B857ABAFE09D99CBFD"), "name": "Jack", "Age":}> db. Student.find ({name: "Jack"},{sex:0}) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Age":} {"_id": ObjectId ("543738B857ABAFE09D99CBFD"), "name": "Jack", "Age":}>

3. Query conditions

"$lt", "$lte", "$gt", "$gte" correspond to <,<=,>,>=
The following code:
Db. Student.find ({age:{$gt: 33}}) query for age greater than 33
Db. Student.find ({age:{$gte: 33}})
Db. Student.find ({age:{$lt: 33}})
Db. Student.find ({age:{$lte: 33}})
Db. Student.find ({age:{$gt:%, $lt: 43}})

> DB. Student.find ({age:{$gt:) {"_id": ObjectId ("543738c357abafe09d99cc00"), "name": "Tom", "Sex": 1, "age": +}> DB . Student.find ({age:{$gte:) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Sex": 1, "age": {"_i" D ": ObjectId (" 543738B857ABAFE09D99CBFD ")," name ":" Jack "," Sex ": 1," age ": {" _id ": ObjectId (" 543738b857abafe09d9 9CBFF ")," name ":" kaily "," Sex ": 0," age ": {" _id ": ObjectId (" 543738c357abafe09d99cc00 ")," name ":" Tom "," Sex ": 1, "Age":}> db. Student.find ({age:{$lt:) {"_id": ObjectId ("543738b857abafe09d99cbfe"), "name": "Lily", "sex": 0, "age":}> D B.student.find ({age:{$lte:) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Sex": 1, "Age": 33} {" _id ": ObjectId (" 543738B857ABAFE09D99CBFD ")," name ":" Jack "," Sex ": 1," age ": ~ {" _id ": ObjectId (" 543738b857abafe09 D99cbfe ")," name ":" Lily "," sex ": 0," age ":} {" _id ": ObjectId (" 543738B857ABAFE09D99CBFF ")," name ":" Kaily"," Sex ": 0," age ":}> db. Student.find ({age:{$gt: $lt: +}}) {"_id": ObjectId ("5437383157ABAFE09D99CBFC"), "name": "Jack", "Sex": 1, "age": 33 } {"_id": ObjectId ("543738B857ABAFE09D99CBFD"), "name": "Jack", "Sex": 1, "age": {"_id": ObjectId ("543738b857aba   FE09D99CBFF ")," name ":" kaily "," Sex ": 0," Age ": 33}

$ne Representative is not equal to
Db. Student.find ({age:{$ne: 33}}) query age is not equal to 33
$in, $not and $or.

Db. Student.find ({age:{$in: [13,53]}}) db. Student.find ({$or:   [    {age:{$in: [13,53]}},    {name: ' kaily '}   ]})  

4, special query--null and exists

Null can match itself, and can match "nonexistent"

--插入测试数据db.Student.insert({name:null,sex:1,age:18})db.Student.insert({sex:1,age:24})db.Student.find({name:null})        --上面两条都能查到db.Student.find({name:{$in:[null],$exists:true}})  ---只能查到第一条   

5. Array data query

6. Embedded document Query

----待完成-------   

7. Sorting

db.Student.find().sort({age:1})db.Student.find().sort({age:1,sex:1})  --1代表升序,-1代表降序

8. Paging

db.Student.find().sort({age:1}).limit(3).skip(3) --limit代表取多少个document,skip代表跳过前多少个document。  

9. Get the quantity

db.Student.count({name:null})   --或者db.Student.find({name:null}).count()

Delete data

说明:删除数据比较简单。db.Student.remove({name:null})

Update data

1. Updating data

命令【db.Student.update({name:"jack"},{age:55})】执行后  先查询name=jack的所有document,然后将name=jack的所有document都替换为{age:55},其它filed都没有了。  正确的更新方式应该为:db.Student.update({name:"jack"},{$set:{age:55}})  

2. Add Field

--将name=lily的student增加一个filed heightdb.Student.update({name:"lily"},{$inc:{height:175}})  

3.upset-How many increments the number field increases

--若存在则添加,否则更新,只能用于数字field,例如age更新前是50,更新了185,则变为235.db.Student.update({name:"lily"},{$inc:{age:185}},true)

4. Batch update data

------------------待完成---------

MongoDB Learning Note (ii)

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.