> db.user.insert({username:'gugang'})
WriteResult({ "nInserted" : 1 })
> db.user.insert({username:'jones'})
WriteResult({ "nInserted" : 1 })
> db.user.find()
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
> db.user.find({username:'gugang'})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
> db.user.find({username:'jones'})
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
> db.user.find({username:'gugang'}).pretty()
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
> db.user.find({$or:[{username:gugang}, {username:jones}]})
2017-06-17T23:47:15.381+0800 E QUERY [thread1] ReferenceError: gugang is not defined :
@(shell):1:21
> db.user.find({$or:[{username:'gugang'}, {username:'jones'}]})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
> db.user.find({$and:[{username:'gugang'}, {username:'jones'}]})
> db.user.find({$and:[{username:'gugang'}, {id:'59454e7b961eb6f4b52b675f'}]})
> db.user.find({$and:[{username:'gugang'}, {-id:'59454e7b961eb6f4b52b675f'}]})
2017-06-18T00:06:14.006+0800 E QUERY [thread1] SyntaxError: invalid property id @(shell):1:42
> db.user.find({$and:[{username:'gugang'}, {_id:'59454e7b961eb6f4b52b675f'}]})
> db.user.find({$and:[{username:'gugang'}, {_id:ObjectId("59454e7b961eb6f4b52b675f")}]})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
> db.user.find({_id:'59454e7b961eb6f4b52b675f'}))
2017-06-18T00:06:59.288+0800 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:46
> db.user.find({'_id':'59454e7b961eb6f4b52b675f'}))
2017-06-18T00:07:12.677+0800 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:48
> db.user.find({$and:[{username:'gugang'}, {_id:ObjectId("59454e7b961eb6f4b52b675f")}]})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
> db.user.find({'_id':''}))
2017-06-18T00:07:32.374+0800 E QUERY [thread1] SyntaxError: missing ; before statement @(shell):1:24
> db.user.find({'_id':''})
> db.user.find({'_id':'59454e7b961eb6f4b52b675f'})
> db.user.insert({_id:1, username:'hello'})
WriteResult({ "nInserted" : 1 })
> db.user.find({'_id':''})
> db.user.find({})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
{ "_id" : 1, "username" : "hello" }
> db.user.find({_id:1})
{ "_id" : 1, "username" : "hello" }
> db.user.update({username:'gugang'}, {$set:{county:"China"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gugang'})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang", "county" : "China" }
> db.user.update({username:'gugang'}, {county:'canada'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gugang'})
> db.user.find({})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "county" : "canada" }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
{ "_id" : 1, "username" : "hello" }
> db.user.update({county:'canada'}, {$set : {username:'gugang'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gugang'})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "county" : "canada", "username" : "gugang" }
> db.user.update({username:'gugang'}, {$unset : {county:1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gugang'})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
> db.user.insert({username:'gavin'})
WriteResult({ "nInserted" : 1 })
> db.user.find({username:'gavin'})
{ "_id" : ObjectId("59455576961eb6f4b52b6761"), "username" : "gavin" }
> db.user.update({username:'gavin'}, {$set : {favorites:{cities:['beijing', 'tianjin'], movies : ['The sting', 'casablanca'] }}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gavin'}).pretty()
{
"_id" : ObjectId("59455576961eb6f4b52b6761"),
"username" : "gavin",
"favorites" : {
"cities" : [
"beijing",
"tianjin"
],
"movies" : [
"The sting",
"casablanca"
]
}
}
> db.user.find({username:'gugang'})
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang" }
> db.user.update({username:'gugang'}, {$set : {favorites : {cities:['shanghai', 'chengdu']}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gugang'}).pertty()
2017-06-18T00:22:04.565+0800 E QUERY [thread1] TypeError: db.user.find(...).pertty is not a function :
@(shell):1:1
> db.user.find({username:'gugang'}).pretty()
{
"_id" : ObjectId("59454e7b961eb6f4b52b675f"),
"username" : "gugang",
"favorites" : {
"cities" : [
"shanghai",
"chengdu"
]
}
}
> db.user.update({username:'gugang'}, {$addToSet:{'favorites.cities':'beijing'}}, false, true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.user.find({username:'gugang'}).pretty()
{
"_id" : ObjectId("59454e7b961eb6f4b52b675f"),
"username" : "gugang",
"favorites" : {
"cities" : [
"shanghai",
"chengdu",
"beijing"
]
}
}
> db.user.find()
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang", "favorites" : { "cities" : [ "shanghai", "chengdu", "beijing" ] } }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
{ "_id" : 1, "username" : "hello" }
{ "_id" : ObjectId("59455576961eb6f4b52b6761"), "username" : "gavin", "favorites" : { "cities" : [ "beijing", "tianjin" ], "movies" : [ "The sting", "casablanca" ] } }
> db.user.remove({_id:1})
WriteResult({ "nRemoved" : 1 })
> db.user.find()
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang", "favorites" : { "cities" : [ "shanghai", "chengdu", "beijing" ] } }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
{ "_id" : ObjectId("59455576961eb6f4b52b6761"), "username" : "gavin", "favorites" : { "cities" : [ "beijing", "tianjin" ], "movies" : [ "The sting", "casablanca" ] } }
> db.user.remove()
2017-06-18T00:27:57.169+0800 E QUERY [thread1] Error: remove needs a query :
DBCollection.prototype._parseRemove@src/mongo/shell/collection.js:409:1
DBCollection.prototype.remove@src/mongo/shell/collection.js:434:18
@(shell):1:1
> db.user.find()
{ "_id" : ObjectId("59454e7b961eb6f4b52b675f"), "username" : "gugang", "favorites" : { "cities" : [ "shanghai", "chengdu", "beijing" ] } }
{ "_id" : ObjectId("59454e85961eb6f4b52b6760"), "username" : "jones" }
{ "_id" : ObjectId("59455576961eb6f4b52b6761"), "username" : "gavin", "favorites" : { "cities" : [ "beijing", "tianjin" ], "movies" : [ "The sting", "casablanca" ] } }
> db.user.remove({})
WriteResult({ "nRemoved" : 3 })
> db.user.find()
>
> show tables
user
> db.user.drop()
true
> show tables
> db.help()