mongodb常用命令

以地理控件索引为例:

插入命令:db.restaurants.insert({name:"Kimono",loc:{type:"Point",coordinates:[52.370451,5.213433]}})

返回值:WriteResult({ "nInserted" : 1 })

db.restaurants.insert({name:"Westblaak",loc:{type:"LineString",coordinates:[[52.370451,4.813433],[52.343423,4.898762]]}})
WriteResult({ "nInserted" : 1 })

 

创建索引:db.restaurants.ensureIndex({loc:"2dsphere"})

返回值:

{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}

 

查看数据库:show dbs

查看集合(相当于关系型数据库中的表):show collections

 

在集合中插入数据:

document=({"Type":"Book","Title":"Definitive Guide to MongoDB","ISBN":"987-3232-2323-23","Publisher":"Apress","Author":["Hows,David","Plugge,Eelco","Membrey,Peter","Hawkins,Tim"]})
{
"Type" : "Book",
"Title" : "Definitive Guide to MongoDB",
"ISBN" : "987-3232-2323-23",
"Publisher" : "Apress",
"Author" : [
"Hows,David",
"Plugge,Eelco",
"Membrey,Peter",
"Hawkins,Tim"
]
}
> db.media.insert(document)
WriteResult({ "nInserted" : 1 })

查询集合中的数据:

db.media.find()
{ "_id" : ObjectId("5655c0fd1e8e824d1d16c3dd"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB", "ISBN" : "987-3232-2323-23", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }

查询指定条件的数据:db.media.find({"Type":"Book"})
{ "_id" : ObjectId("5655c0fd1e8e824d1d16c3dd"), "Type" : "Book", "Title" : "Definitive Guide to MongoDB", "ISBN" : "987-3232-2323-23", "Publisher" : "Apress", "Author" : [ "Hows,David", "Plugge,Eelco", "Membrey,Peter", "Hawkins,Tim" ] }
>

返回指定键的名称:

db.media.find({"Type":"Book"},{Title:1,Publisher:1})
{ "_id" : ObjectId("5655c0fd1e8e824d1d16c3dd"), "Title" : "Definitive Guide to MongoDB", "Publisher" : "Apress" }

今天先到这里:下面继续4.3.2使用函数sort、limit、skip

posted @ 2015-11-25 22:16  AntColony  阅读(139)  评论(0)    收藏  举报