标签:
1,初始化数据库,插入数据:
doc=({"name": "peter", "position": "teacher"})
{ "name" : "peter", "position" : "teacher" }
> db.shiyanlou.insert(doc)
> doc1=({"name": "tom", "position": "student"})
{ "name" : "tom", "position" : "student" }
> db.shiyanlou.insert(doc1)
> db.shiyanlou.find()
{ "_id" : ObjectId("5559d1cca30df8c25bf44dd7"), "name" : "peter", "position" :
"teacher" }
{ "_id" : ObjectId("5559d1e9a30df8c25bf44dd8"), "name" : "tom", "position" : "
student" }
>
db.shiyanlou.find({"name": {$type:2}})
{ "_id" : ObjectId("5559d1cca30df8c25bf44dd7"), "name" : "peter", "position" :
"teacher" }
{ "_id" : ObjectId("5559d1e9a30df8c25bf44dd8"), "name" : "tom", "position" : "
student" }
> db.shiyanlou.find().limit(1)
{ "_id" : ObjectId("5559d1cca30df8c25bf44dd7"), "name" : "peter", "position" :
"teacher" }
>
> db.shiyanlou.find().limit(1).skip(2)
> db.shiyanlou.find().limit(1).skip(1)
{ "_id" : ObjectId("5559d1e9a30df8c25bf44dd8"), "name" : "tom", "position" : "
student" }
db.shiyanlou.find().sort({"name": 1})
{ "_id" : ObjectId("5559d1cca30df8c25bf44dd7"), "name" : "peter", "position" :
"teacher" }
{ "_id" : ObjectId("5559d1e9a30df8c25bf44dd8"), "name" : "tom", "position" : "
student" }
> db.shiyanlou.find().sort({"name":-1})
{ "_id" : ObjectId("5559d1e9a30df8c25bf44dd8"), "name" : "tom", "position" : "
student" }
ensureIndex()的可选参数:
参数 | 类型 | 描述 |
---|---|---|
background | Boolean | 建立索引要不要阻塞其他数据库操作,默认为false |
unique | Boolean | 建立的索引是否唯一,默认false |
name | string | 索引的名称,若未指定,系统自动生成 |
dropDups | Boolean | 建立唯一索引时,是否删除重复记录,默认flase |
sparse | Boolean | 对文档不存在的字段数据不启用索引,默认false |
expireAfterSeconds | integer | 设置集合的生存时间,单位为秒 |
v | index version | 索引的版本号 |
weights | document | 索引权重值,范围为1到99999 |
default-language | string | 默认为英语 |
language_override | string | 默认值为 language |
标签:
原文地址:http://www.cnblogs.com/jingLongJun/p/4513035.html