C:\Users\issuser>mongo
MongoDB shell version: 2.4.6
connecting to: test
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({"age":{$gt:22}})
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({"age":{$lt:22}})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
> db.user.find({"age":{$ne:22}})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({"age":20})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
> db.user.find({"name":"jack","address.province":"anhui"})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
> db.user.find({$or:[{"address.province":"anhui"},{"address.province":"anhui"},{"address.province":"guangdong"}]})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({$or:[{"address.province":"anhui"},{"address.province":"anhui"},{"address.province":"jiansu"}]})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({"address.province":{$in:["anhui","jiansu"]}})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({"address.province":{$nin:{"anhui","jiansu"}}})
Thu Jan 12 10:50:04.387 SyntaxError: Unexpected token ,
> db.user.find({"address.province":{$nin:{"anhui"}}})
Thu Jan 12 10:51:03.163 SyntaxError: Unexpected token }
> db.user.find({"address.province":{$nin:["anhui"]}})
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({"address.province":{$nin:["anhui","jiansu"]}})
> db.user.find({"name":/^j/,"name":/e$})
Thu Jan 12 10:54:24.025 SyntaxError: Invalid regular expression: missing /
> db.user.find({"name":/^j/,"name":/e$/})
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.find({$where:function(){return this.name=='jack'}})
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 20, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
> db.user.find({$where:function(){return this.name=='joe'}})
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> var model=db.user.find({"name":"jack"})
> model.age=30
30
> db.user.update({"name":"jack"},model)
Thu Jan 12 11:00:09.924 can't save a DBQuery object at src/mongo/shell/collection.js:134
> var model=db.user.findOne({"name":"jack"})
> model.age=30
30
> db.user.update({"name":"jack"},model)
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 30, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.update({"name","jack"},{$inc:{"age":30}})
Thu Jan 12 11:08:20.891 SyntaxError: Unexpected token ,
> db.user.update({"name":"jack"},{$inc:{"age":30}})
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 60, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.update({"name":"jack"},{$set:{"age":10}})
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 10, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
> db.user.update({"name":"jackson"},{$inc{"age":1},true})
Thu Jan 12 11:15:18.691 SyntaxError: Unexpected token {
> db.user.update({"name":"jackson"},{$inc{"age":1}},true)
Thu Jan 12 11:15:36.786 SyntaxError: Unexpected token {
> db.user.update({"name":"jackson"},{$inc:{"age":1}},true)
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 10, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
{ "_id" : ObjectId("5876f4fc01437187c9e8894b"), "age" : 1, "name" : "jackson" }
> db.user.update({"name":"jack"},{$set:{"age":56}},true,true)
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 56, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
{ "_id" : ObjectId("5876f4fc01437187c9e8894b"), "age" : 1, "name" : "jackson" }
> db.user.update({"name":"/^j/"},{$set:{"age":36}},true,true)
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 56, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 25, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
{ "_id" : ObjectId("5876f4fc01437187c9e8894b"), "age" : 1, "name" : "jackson" }
{ "_id" : ObjectId("5876f64001437187c9e8894c"), "age" : 36, "name" : "/^j/" }
> db.user.update({"name":/^j/},{$set:{"age":36}},true,true)
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 36, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 36, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
{ "_id" : ObjectId("5876f4fc01437187c9e8894b"), "age" : 36, "name" : "jackson" }
{ "_id" : ObjectId("5876f64001437187c9e8894c"), "age" : 36, "name" : "/^j/" }
> db.user.remove({"name":"/^j/"}))
> db.user.find()
{ "_id" : ObjectId("5875f6407c149d4b6063e99d"), "name" : "jack", "password" : "12345", "age" : 36, "address" : { "province" : "anhui", "city" : "hefei" }, "favourite" : [ "apple", "banana" ] }
{ "_id" : ObjectId("5875f6e57c149d4b6063e99e"), "name" : "joe", "password" : "12345", "age" : 36, "address" : { "province" : "jiansu", "city" : "nanjing" }, "favourite" : [ "money", "mm" ] }
{ "_id" : ObjectId("5876f4fc01437187c9e8894b"), "age" : 36, "name" : "jackson" }
> db.person.find()
{ "_id" : ObjectId("5875f3547c149d4b6063e99b"), "name" : "jack", "age" : 20 }
> db.person.insert({"name":"chun1","age":28})
> db.person.insert({"name":"hh2","age":27})
> db.person.insert({"name":"yudie",age:5})
> db.person.insert({"name":"caiyi",age:3})
> db.person.find()
{ "_id" : ObjectId("5875f3547c149d4b6063e99b"), "name" : "jack", "age" : 20 }
{ "_id" : ObjectId("5876fa7cd039ee6fe7076301"), "name" : "chun1", "age" : 28 }
{ "_id" : ObjectId("5876fa90d039ee6fe7076302"), "name" : "hh2", "age" : 27 }
{ "_id" : ObjectId("5876faa3d039ee6fe7076303"), "name" : "yudie", "age" : 5 }
{ "_id" : ObjectId("5876fab8d039ee6fe7076304"), "name" : "caiyi", "age" : 3 }
> db.person.count()
5
> db.person.count({"name":"jack"})
1
> db.person.distinct("age")
[ 20, 28, 27, 5, 3 ]
> db.person.update({"name":"hh2"},{"age":28})
> db.person.distinct("age")
[ 20, 28, 5, 3 ]
> db.person.count({"age":28})
2
> db.person.group({})
Thu Jan 12 13:54:05.987 group command failed: { "ok" : 0, "errmsg" : "$reduce has to be set" } at src/mongo/shell/db.js:655
> db.person.group({})
Thu Jan 12 13:54:17.592 group command failed: { "ok" : 0, "errmsg" : "$reduce has to be set" } at src/mongo/shell/db.js:655
> db.person.group({//})
...
...
> db.persong.group({//
... "key":{"age":true},//
... "initial":{"person":[]},//
... "$reduce":function(cur,prev){prev.person.push(cur.name);}})
[ ]
> db.person.find()
{ "_id" : ObjectId("5875f3547c149d4b6063e99b"), "name" : "jack", "age" : 20 }
{ "_id" : ObjectId("5876fa7cd039ee6fe7076301"), "name" : "chun1", "age" : 28 }
{ "_id" : ObjectId("5876fa90d039ee6fe7076302"), "age" : 28 }
{ "_id" : ObjectId("5876faa3d039ee6fe7076303"), "name" : "yudie", "age" : 5 }
{ "_id" : ObjectId("5876fab8d039ee6fe7076304"), "name" : "caiyi", "age" : 3 }
> db.person.group({// "key":{"age":true},// "initial":{"person":[]},// "$reduce":function(cur,prev){prev.person.push(cur.name);}})
...
...
>
> db.person.group({"key":{"age":true},"initial":{"person":[]},"$reduce":function(cur,prev){prev.person.push(cur.name);}})
[
{
"age" : 20,
"person" : [
"jack"
]
},
{
"age" : 28,
"person" : [
"chun1",
null
]
},
{
"age" : 5,
"person" : [
"yudie"
]
},
{
"age" : 3,
"person" : [
"caiyi"
]
}
]
> db.person.group({"key":true},"initial":{"person":[]},"reduce":function(doc,out){out.person.push(doc.name);},"finalize":function(out){out.count=out.persong.length;},"condition":{"age":{$lt:25}}})
Thu Jan 12 15:33:53.393 SyntaxError: Unexpected token :
> db.person.group({"key":true},"initial":{"person":[]},"reduce":function(doc,out){out.person.push(doc.name);},"finalize":function(out){out.count=out.persong.length;},"condition":{"age":{$lt:25}})
Thu Jan 12 15:33:58.400 SyntaxError: Unexpected token :
> db.person.group({"key":{"age":true},"initial":{"person":[]},"reduce":function(doc,out){out.person.push(doc.name);},"finalize":function(out){out.count=out.persong.length;},"condition":{"age":{$lt:25}}})
Thu Jan 12 15:38:50.740 group command failed: {
"errmsg" : "exception: TypeError: Cannot read property 'length' of undefined at $group finalize define",
"code" : 16722,
"ok" : 0
} at src/mongo/shell/db.js:655
> db.person.group({"key":{"age":true},"initial":{"person":[]},"reduce":function(doc,out){out.person.push(doc.name);},"finalize":function(out){out.count=out.person.length;},"condition":{"age":{$lt:25}}})
[
{
"age" : 20,
"person" : [
"jack"
],
"count" : 1
},
{
"age" : 5,
"person" : [
"yudie"
],
"count" : 1
},
{
"age" : 3,
"person" : [
"caiyi"
],
"count" : 1
}
]
>