mongoldb CRUD 操作

本文展示了如何使用MongoDB进行基本的操作,包括插入、查询、更新和删除文档等。通过具体的命令示例,读者可以了解MongoDB的基本用法,如使用$or和$and进行复合查询,使用$set和$unset修改字段,以及使用$addToSet添加数组元素。
摘要由CSDN通过智能技术生成
> 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()


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值