您的位置:首页 > 数据库 > Mongodb

mongodb操作--文档、集合、数据库、shell

2017-01-22 18:16 736 查看
MongoDB中的集合类似于关系型数据库中的表,mongodb中的文档类似于关系型数据库中的行。关系型数据库中的一条记录就是一个文档,是一个数据结构,由field和value对组成。MongoDB文档与JSON对象类似。字段的值有可能包括其它文档、数组以及文档数组。多个文档组成一个集合, 多个集合组成一个数据库。

这里mongdb版本是3.2



常用命令

创建一个集合

use MyDB;
// 如果MyDB和集合都不存在,都会自动创建
db.collections1.insert({name : 'test'});




显示所有DB

show dbs 显示DB列表以及存储状况




查找某个DB下面的所有集合

// 查找某个DB下面的collection(集合)名称
use 'db名称' // 使用具体的DB
 show collections;




删除一个DB

use MyDB
db.dropDatabase();




删除一个集合

usr MyDB
show collections;
db.collection1.drop();




插入一个文档

测试数据
db.users.insertMany(
[
{
_id: 1,
name: "sue",
age: 19,
type: 1,
status: "P",
favorites: { artist: "Picasso", food: "pizza" },
finished: [ 17, 3 ],
badges: [ "blue", "black" ],
points: [
{ points: 85, bonus: 20 },
{ points: 85, bonus: 10 }
]
},
{
_id: 2,
name: "bob",
age: 42,
type: 1,
status: "A",
favorites: { artist: "Miro", food: "meringue" },
finished: [ 11, 25 ],
badges: [ "green" ],
points: [
{ points: 85, bonus: 20 },
{ points: 64, bonus: 12 }
]
},
{
_id: 3,
name: "ahn",
age: 22,
type: 2,
status: "A",
favorites: { artist: "Cassatt", food: "cake" },
finished: [ 6 ],
badges: [ "blue", "red" ],
points: [
{ points: 81, bonus: 8 },
{ points: 55, bonus: 20 }
]
},
{
_id: 4,
name: "xi",
age: 34,
type: 2,
status: "D",
favorites: { artist: "Chagall", food: "chocolate" },
finished: [ 5, 11 ],
badges: [ "red", "black" ],
points: [
{ points: 53, bonus: 15 },
{ points: 51, bonus: 15 }
]
},
{
_id: 5,
name: "xyz",
age: 23,
type: 2,
status: "D",
favorites: { artist: "Noguchi", food: "nougat" },
finished: [ 14, 6 ],
badges: [ "orange" ],
points: [
{ points: 71, bonus: 20 }
]
},
{
_id: 6,
name: "abc",
age: 43,
type: 1,
status: "A",
favorites: { food: "pizza", artist: "Picasso" },
finished: [ 18, 12 ],
badges: [ "black", "blue" ],
points: [
{ points: 78, bonus: 8 },
{ points: 57, bonus: 7 }
]
}
]
)


db.collection.insertOne(); // 插入一个文档
db.collection.insertMany(); // 插入多个文档
db.collection.insert()  // 可以插入一个集合或多个文档




查询集合中所有文档

// 语法格式 db.collection.find();
db.users.find();




// 按条件查询

// 语法格式 db.collection.find({'field':'value'})
db.users.find({status:'A'});


// 返回指定的字段及 _id 字段

// 语法格式 db.collection.find({条件}, {指定查询的字段}),指定查询的字段值如果为true,出现在查询的结果中,false反之不出现

// 把status=A作为查询条件,指定name,status这两列查询出来
db.users.find({status: 'A'}, {name:true, status:true});




除了ID字段你可以

从查询结果中排除一个或多个字段,把值设为false即可



下面的示例指定了一个映射,返回 _id 字段、name 字段、 status 字段以及 favorites 文档中的 food 字段, food 仍然保持嵌入在 favorites 文档中。

db.users.find({"status":"A"}, {name:true, status:true, "favorites.food":true});




文档中嵌套子文档查询方式,比如下列数据结构



db.users.find({_id:1, "favorites.food":"pizza"},{name:true, age:true, "favorites.food":true})




映射返回数组中特定的数组元素

对于包含数组的字段,MongoDB提供了下面的映射操作符: elemMatch,slice, 以及 $ 。

下面的示例使用 $slice 映射操作符来返回 scores 数组中最后的元素。

不指定条件返回所有元素



如果我们需要指定返回数组中最后一个元素, 可以使用{slice : -1}
查询数组第一个元素{slice : 1},第二个{$slice : 2},以此类推

db.users.find({status : 'A'}, {name:true, status:true, points: {$slice: -1}})




查询值为Null或不存在的字段

MongoDB中不同的运算符对待 null 值不一样.

db.users.insert(
[
{ "_id" : 900, "name" : null },
{ "_id" : 901 }
]
)


db.users.find( { name: null } )


该查询返回这两个文档:

{ "_id" : 900, "name" : null }
{ "_id" : 901 }


类型筛查

{ name : { $type: 10 } } 查询 仅仅 匹配那些包含值是 null 的 name 字段的文档,亦即 条目 字段的值是BSON类型中的 Null (即 10 ):

db.users.find({name : {$type : 10}})


该查询只返回字段是 null 值的文档:

{ "_id" : 900, "name" : null }




查询匹配的文档中不含的字段值

语法:field:{$exists:false}

// 这里查询结果中不包含name字段的文档
db.users.find({name:{$exists:false}})




更新文档

使用 $set 操作符更新 favorites.food 字段的值到 "test"并更新 ``type 字段的值到 3.

语法:db.collection.updateOn({'field':value'},{$set : {field:'value'}})
使用updateOne更新一个文档
db.users.updateOne({"favorites.food":'yangli'},{$set : {"favorites.food":'test', type : 3}})


未更新前文档记录



更新后结果



下面的例子对 users 集合使用 db.collection.update() 方法来更新匹配过滤条件– favorites.artist 等于 “Picasso” 的 第一个 文档.更新操作:

使用 $set 操作符更新 favorites.food 字段的值到 “pizza”
并更新
type 字段的值到 0,

使用 currentDate操作符更新lastModified字段的值到当前日期.如果lastModified字段不存在,currentDate 会创建该字段.详情请参阅 $currentDate.

db.collection.update

db.users.update(
{ "favorites.artist": "Picasso" },
{
$set: { "favorites.food": "pizza", type: 0,  },
$currentDate: { lastModified: true }
}
)


更新多条记录

db.users.update(
{ "favorites.artist": "Picasso" },
{
$set: { "favorites.food": "pizza", type: 0,  },
$currentDate: { lastModified: true }
},
{ multi: true }
)




记录替换

下面替换_id为1的文档

db.users.replaceOne(
{ _id : 1},
{name : "yangli", age: 100, type:200, status : "p", favorites: ["red", "gree"]}
)


替换结果



文档删除

1:删除所有文档命令

db.users.deleteMany({}) 或 db.users.remove({});


该方法返回了操作状态的文档:

{ "acknowledged" : true, "deletedCount" : 6 }




2:按条件删除

db.collection.deleteMany() 方法或 db.collection.remove() 方法.

// 下面删除name:abc的文档
db.users.deleteMany({name : 'abc'})
// 结果
{ "acknowledged" : true, "deletedCount" : 1 }




// 删除status等于A的所有文档
db.users.remove({status: 'A'});

// 返回结果
WriteResult({ "nRemoved" : 2 })




仅删除一个满足条件的文档

要想最多删除一个满足指定过滤条件的文档(即使多个文档可以满足该指定过滤条件),使用 db.collection.deleteOne() 方法或使用 db.collection.remove() 方法并将 <justOne> 参数设置为 true 或 1.

db.users.remove({type : 2},true)

// 返回结果
WriteResult({ "nRemoved" : 1 })


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息