1. 插入扩展 > use test
switched to db test >db.post.insert([
{
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": 100
},
{
"title": "NoSQL Database",
"description": "NoSQL database doesn't have tables",
"by": "tutorials point",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": 20,
"comments": [
{
"user":"user1",
"message": "My first comment",
"date": new Date(),
"like": 0
}
]
}
]) |
这里的insert插入多个文档用[] 这里还有数组和内嵌文档 2. 查询扩展 > db.post.find().pretty()
{
"_id" : ObjectId("53f6e3e878a0a91d1242f0ea"),
"0" : {
"title" : "MongoDB Overview",
"description" : "MongoDB is no sql database",
"by" : "tutorials point",
"url" : "http://www.yiibai.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 100
},
"1" : {
"title" : "NoSQL Database",
"description" : "NoSQL database doesn't have tables",
"by" : "tutorials point",
"url" : "http://www.yiibai.com",
"tags" : [
"mongodb",
"database",
"NoSQL"
],
"likes" : 20,
"comments" : [
{
"user" : "user1",
"message" : "My first comment",
"date" : "Fri Aug 22 2014 14:32:08 GMT+0800 (CST)",
"like" : 0
}
]
}
} |
这里的pretty是格式化输出 条件查询”$lt”、”$lte”、”$gt”、”$gte”对应的是<、<=、>、>= 指定查找范围"$in”反之"$nin" 和oracle中的in和not in含义相同 AND和OR ,AND的话直接在()里面填入键值对 OR用”$or” 下面是准备数据: > db.post.drop() >db.post.insert(
{
"title": "MongoDB Overview",
"description": "MongoDB is no sql database",
"by": "tutorials point",
"url": "http://www.yiibai.com",
"tags": ["mongodb", "database", "NoSQL"],
"likes": 100
}
) |
等值查询: >db.post.find({"likes":100}).pretty()
|
条件查询: > db.post.find({"likes":{"$lt":200}}).pretty() 其他的条件查询一样
|
指定查找范围: > db.post.find({"likes":{"$in":[100,50,200]}}).pretty()
|
AND查询: > db.post.find({"likes":100,"description":"MongoDB is no sql database"}).pretty()
|
OR查询: > db.post.find({"$or":[{"likes":100},{"description":"MongoDB is no sql database"}]}).pretty()
|
模糊查询:像oracle中的like只是这里用的shell方式,如awk,sed等 > db.post.find({"description":/^mongo/i}).pretty()
|
3. 更新扩展 “$set” > db.post.update({"likes":100},{"$set":{"likes": 50}}) 修改likes为50 |
“$inc” > db.post.update({"likes":50},{"$inc":{"likes": 100}}) 在原来的基础上增加100,仅支持数字
|