Linux-MongoDB(二)基础命令

1、集合

增加集合

// db.createCollection(name, options)
db.createCollection('collection1')

// capped为true则创建固定大小的集合
// autoIndexId为true则在创建文档的时候自动生成_id
// size指定一个最大容量,如果capped为true需要指定该字段
// max指定固定集合中包含文档的最大数量
db.createCollection('collection2',{capped: true, autoIndexId: true, size: 1024, max: 1000})

// 插入文档时自动创建集合
db.collection3.insert({name: '为了创建collection3'})

查看所有集合

> show collections
collection1
collection2
collection3

> db.getCollectionNames()
[ "collection", "collection1", "collection2", "collection4" ]

删除集合

// db.COLLECTION_NAME.drop()
db.collection3.drop()

2、文档

不带参数创建文档

// db.collectionName.insert(<document>)

> db.student.insert({name: 'tao'})
WriteResult({ "nInserted" : 1 })

> db.student.insert([{name: 'liu'},{name: 'hu'}])
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

> db.student.insert({name: 'huyang', age: 18, score: [{math: 80}, {chinese: 90}]})
WriteResult({ "nInserted" : 1 })

// 自定义_id
> db.student.insert({_id: 1, name: 'lucy'})
WriteResult({ "nInserted" : 1 })
> db.student.find({_id: 1})
{ "_id" : 1, "name" : "lucy" }

带参数创建

db.collection.insert(
	<document>,
	{
		//可选字段
		writeConcern:<options> //  w: <value>, j: <boolean>, wtimeout: <number>
		ordered: <boolean> // true: 默认,报错时后续文档不再插入/false: 后续文档继续插入
	}
)

// w:<number> 1:默认值,应带式写入;0:非应带式写入;>0:用于写入节点的数量,包括master
> db.student.insert({_id: 2, name: 'lucy'}, {writeConcern: {w: 0}})
WriteResult({ })

// j:在写入journal日志后应带客户端(需要开启journal功能)

// wtimeout:单位毫秒,写入超市返回错误

> db.student.insert([{_id: 2, name: 's1'}, {_id: 3, name: 's2'}, {_id: 4, name: 's3'}], {ordered: true})
BulkWriteResult({
	"writeErrors" : [
		{
			"index" : 0,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error collection: test.student index: _id_ dup key: { _id: 2.0 }",
			"op" : {
				"_id" : 2,
				"name" : "s1"
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 0,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

> db.student.insert([{_id: 2, name: 's1'}, {_id: 3, name: 's2'}, {_id: 4, name: 's3'}], {ordered: false})
BulkWriteResult({
	"writeErrors" : [
		{
			"index" : 0,
			"code" : 11000,
			"errmsg" : "E11000 duplicate key error collection: test.student index: _id_ dup key: { _id: 2.0 }",
			"op" : {
				"_id" : 2,
				"name" : "s1"
			}
		}
	],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})


插入单条数据

> db.student.insertOne({name: 'Tong'})
{
	"acknowledged" : true,
	"insertedId" : ObjectId("643effd1a57961cf461b183e")
}

插入多条数据

> db.student.insertMany([{name: 'hu'},{name: 'luo'}])
{
	"acknowledged" : true,
	"insertedIds" : [
		ObjectId("643f0000a57961cf461b183f"),
		ObjectId("643f0000a57961cf461b1840")
	]
}

插入变量

> student={name: 'nancy'}
{ "name" : "nancy" }
> db.student.insert(student)
WriteResult({ "nInserted" : 1 })

> students=[{name: 's1'}, {name: 's2'}]
[ { "name" : "s1" }, { "name" : "s2" } ]
> db.student.insert(students)
BulkWriteResult({
	"writeErrors" : [ ],
	"writeConcernErrors" : [ ],
	"nInserted" : 2,
	"nUpserted" : 0,
	"nMatched" : 0,
	"nModified" : 0,
	"nRemoved" : 0,
	"upserted" : [ ]
})

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值