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" : [ ]
})