MongoDB基础

Posted xingweikun

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MongoDB基础相关的知识,希望对你有一定的参考价值。

MongoDB 创建数据库

创建xwk数据库

> use xwk
switched to db xwk

查看所有数据库

> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB

可以看到,我们刚创建的数据库 xwk 并不在数据库的列表中, 要显示它,我们需要向 xwk 数据库插入一些数据。

> db.xwk.insert({'name':'tom','age':20})
WriteResult({ "nInserted" : 1 })
> show dbs
admin   0.000GB
config  0.000GB
local   0.000GB
test    0.000GB
xwk     0.000GB
>

删除数据库

删除当前数据库

db.dropDatabase()

创建集合

语法格式:

db.createCollection(name, options)

创建students集合

> db.createCollection("students")
{ "ok" : 1 }
>

查看已有集合

> show collections
students
xwk
>

删除集合

> db.xwk.drop()
true
> show collections
students
>

MongoDB 插入文档

MongoDB 使用 insert() 或 save() 方法向集合中插入文档,语法如下:

db.COLLECTION_NAME.insert(document)
或
db.COLLECTION_NAME.save(document)

save():如果 _id 主键存在则更新数据,如果不存在就插入数据。该方法新版本中已废弃,可以使用 db.collection.insertOne()db.collection.replaceOne() 来代替。

insertOne()插入一条数据
insertMany()插入多条数据

语法格式如下:

db.collection.insertOne(
   <document>,
   {
      writeConcern: <document>
   }
)
db.collection.insertMany(
   [ <document 1> , <document 2>, ... ],
   {
      writeConcern: <document>,
      ordered: <boolean>
   }
)
参数说明:

document:要写入的文档。
writeConcern:写入策略,默认为 1,即要求确认写操作,0 是不要求。
ordered:指定是否按顺序写入,默认 true,按顺序写入。
> db.students.insert({title:'学习MongoDB',
... description:'MongoDB是一个Nosql数据库',
... by:'xingweikun',
... tags:['mongodb','databse','nosql']
... })
WriteResult({ "nInserted" : 1 })

查看已插入文档

> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
>

我们也可以将数据定义为一个变量

> data1={'name':'mongodb','grade':100}
{ "name" : "mongodb", "grade" : 100 }
> db.students.insert(data1)
WriteResult({ "nInserted" : 1 })
> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
>

插入单条数据

> var data2=db.students.insertOne({'name':'aaa'})
> data2
{
        "acknowledged" : true,
        "insertedId" : ObjectId("618bcc5c80e8165008ca7e21")
}
> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : ObjectId("618bcc5c80e8165008ca7e21"), "name" : "aaa" }
>

插入多条数据

> var data3=db.students.insertMany([{'name':'bbb','age':18}])
> data3
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("618bcd0180e8165008ca7e22")
        ]
}
> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : ObjectId("618bcc5c80e8165008ca7e21"), "name" : "aaa" }
{ "_id" : ObjectId("618bcd0180e8165008ca7e22"), "name" : "bbb", "age" : 18 }
>
> db.students.insertMany([
... {_id:1,name:'xwk',age:20},
... {_id:2,name:'ccc',age:18}
... ]);
{ "acknowledged" : true, "insertedIds" : [ 1, 2 ] }
> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : ObjectId("618bcc5c80e8165008ca7e21"), "name" : "aaa" }
{ "_id" : ObjectId("618bcd0180e8165008ca7e22"), "name" : "bbb", "age" : 18 }
{ "_id" : 1, "name" : "xwk", "age" : 20 }
{ "_id" : 2, "name" : "ccc", "age" : 18 }
>

MongoDB 更新文档

语法格式如下

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation
   }
)
参数说明:

query : update的查询条件,类似sql update查询内where后面的。
update : update的对象和一些更新的操作符(如$,$inc...)等,也可以理解为sql update查询内set后面的
upsert : 可选,这个参数的意思是,如果不存在update的记录,是否插入objNew,true为插入,默认是false,不插入。
multi : 可选,mongodb 默认是false,只更新找到的第一条记录,如果这个参数为true,就把按条件查出来多条记录全部更新。
writeConcern :可选,抛出异常的级别。
collation:指定语言

将_id为2的name的值改为update

> db.students.update(
... {
... _id:2
... },
... {
... $set:{name:'update'}
... }
... )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : ObjectId("618bcc5c80e8165008ca7e21"), "name" : "aaa" }
{ "_id" : ObjectId("618bcd0180e8165008ca7e22"), "name" : "bbb", "age" : 18 }
{ "_id" : 1, "name" : "xwk", "age" : 20 }
{ "_id" : 2, "name" : "update", "age" : 18 }
>

创建bjx集合,指定中文

> db.createCollection("bjx",{collation:{locale:"zh"}})//创建集合并指定语言
{ "ok" : 1 }
> show collections                                               指定语言
bjx
students
> db.bjx.insertMany([ {_id:1,n:'赵'}, {_id:2,n:'钱'}, {_id:3,n:'孙'}, {_id:4,n:'李'} ]);
{ "acknowledged" : true, "insertedIds" : [ 1, 2, 3, 4 ] }
> db.bjx.find()
{ "_id" : 1, "n" : "赵" }
{ "_id" : 2, "n" : "钱" }
{ "_id" : 3, "n" : "孙" }
{ "_id" : 4, "n" : "李" }

按中文排序

> db.bjx.find().sort({n:1})
{ "_id" : 4, "n" : "李" }
{ "_id" : 2, "n" : "钱" }
{ "_id" : 3, "n" : "孙" }
{ "_id" : 1, "n" : "赵" }
>

MongoDB 删除文档

> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : ObjectId("618bcc5c80e8165008ca7e21"), "name" : "aaa" }
{ "_id" : ObjectId("618bcd0180e8165008ca7e22"), "name" : "bbb", "age" : 18 }
{ "_id" : 1, "name" : "xwk", "age" : 20 }
{ "_id" : 2, "name" : "update", "age" : 18 }
> db.students.remove({'name':'aaa'})
WriteResult({ "nRemoved" : 1 })
> db.students.remove({'name':'bbb'})
WriteResult({ "nRemoved" : 1 })
> db.students.find()
{ "_id" : ObjectId("618bcab580e8165008ca7e1f"), "title" : "学习MongoDB", "description" : "MongoDB是一个Nosql数据库", "by" : "xingweikun", "tags" : [ "mongodb", "databse", "nosql" ] }
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : 1, "name" : "xwk", "age" : 20 }
{ "_id" : 2, "name" : "update", "age" : 18 }
>

删除title='学习MongoDB’的一个文档

> db.students.deleteOne({title:'学习MongoDB'})
{ "acknowledged" : true, "deletedCount" : 1 }
> db.students.find()                        })
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : 1, "name" : "xwk", "age" : 20 }
{ "_id" : 2, "name" : "update", "age" : 18 }
>
> db.students.insert({name:'ddd',age:18})
WriteResult({ "nInserted" : 1 })
> db.students.find()
{ "_id" : ObjectId("618bcbbe80e8165008ca7e20"), "name" : "mongodb", "grade" : 100 }
{ "_id" : 1, "name" : "xwk", "age" : 20 }
{ "_id" : 2, "name" : "update", "age" : 18 }
{ "_id" : ObjectId("618bd69180e8165008ca7e24"), "name" : "ddd", "age" : 18 }
>

删除集合下的所有文档

> db.students.deleteMany({})
{ "acknowledged" : true, "deletedCount" : 4 }
> db.students.find()
>

删除age=18的全部文档

> db.students.insert({name:'ddd',age:18})  
WriteResult({ "nInserted" : 1 })
> db.students.insert({name:'aaa',age:18})
WriteResult({ "nInserted" : 1 })
> db.students.insert({name:'ccc',age:18})
WriteResult({ "nInserted" : 1 })
> db.students.find()
{ "_id" : ObjectId("618bd75b80e8165008ca7e25"), "name" : "ddd", "age" : 18 }
{ "_id" : ObjectId("618bd76280e8165008ca7e26"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("618bd76a80e8165008ca7e27"), "name" : "ccc", "age" : 18 }
> db.students.deleteMany({age:18})
{ "acknowledged" : true, "deletedCount" : 3 }
> db.students.find()
>

MongoDB 查询文档

以格式化的方式来显示所有文档

> db.bjx.find().pretty()
{ "_id" : 1, "n" : "赵" }
{ "_id" : 2, "n" : "钱" }
{ "_id" : 3, "n" : "孙" }
{ "_id" : 4, "n" : "李" }

新插入数据

> db.students.insertMany([
... {name:'aaa',age:18},
... {name:'bbb',age:20},
... {name:'ccc',age:22},
... {name:'ddd',age:24},
... {name:'eee',age:26},
... {name:'fff',age:28},
... {name:'ggg',age:30}
... ]);
{
        "acknowledged" : true,
        "insertedIds" : [
                ObjectId("618bd98080e8165008ca7e28"),
                ObjectId("618bd98080e8165008ca7e29"),
                ObjectId("618bd98080e8165008ca7e2a"),
                ObjectId("618bd98080e8165008ca7e2b"),
                ObjectId("618bd98080e8165008ca7e2c"),
                ObjectId("618bd98080e8165008ca7e2d"),
                ObjectId("618bd98080e8165008ca7e2e")
        ]
}
> db.students.find()
{ "_id" : ObjectId("618bd98080e8165008ca7e28"), "name" : "aaa", "age" : 18 }
{ "_id" : ObjectId("618bd98080e8165008ca7e29"), "name" : "bbb", "age" : 20 }
{ "_id" : ObjectId("618bd98080e8165008ca7e2a"), "name" : "ccc", "age" : 22 }
{ "_id" : ObjectId("618bd98080e8165008ca7e2b"), "name" : "ddd", "age" : 24 }
{ "_id" : ObjectId("618bd98080e8165008ca7e2c"), "name" : "eee", "age" : 26 }
{ "_id" : ObjectId("618bd98080e8165008ca7e2d"), "name" : "fff", "age" : 28 }
{ "_id" : ObjectId("618bd98080e8165008ca7e2e"), "name" : "ggg", "age" : 30 }
>

查询条件

> db.students.find({age:24})//age=24
{ "_id" : ObjectId("618bd98080e8165008ca7e2b"), "name" : "ddd", MongoDB PHP

ios - Heroku 和 MongoDb 上的自定义解析服务器错误 3080:JSON 文本没有以数组或对象开头,并且允许未设置片段的选项

[Go] 通过 17 个简短代码片段,切底弄懂 channel 基础

mongodb关联查询

无法在 MongoDB(猫鼬)文档中追加数组

201555332盛照宗—网络对抗实验1—逆向与bof基础