命令行连接
mongo -u 用户名 -p 密码 --port 端口号 --host IP 数据库名
show dbs;
use 数据库名;
show collections;
db.集合名.find()
基本语句
// 插入不指定 _id 字段的文档的代码如下:
db.test.insert( { item : "card", qty : 15 })
// 查询不包含
db.getCollection('core_user').find({icon:/^(?!.*none.png).*/})
db.getCollection('core_pack').find({}) //查询所有
db.getCollection('core_user').find({"departmentMap":{$ne:{}}},{"username":1,"deptName":1,"departmentMap":1})
db.getCollection('core_user').find({"tags.0":{$exists:1}}) //查询数组长度大于0
db.getCollection('core_homepage').find({"createdDate":{"$lt":ISODate("2021-05-28T01:16:33.303Z")}}).sort({"sortNo":-1}) //查询日期
db.getCollection('third_user').update({"usedSize":0},{$unset:{"diskSize":""}},{"multi":true}) //删除diskSize字段
db.getCollection('core_user').update({"username":{$in:["ceshi1","ceshi2","ceshi4","ceshi5","ceshi6"]}},{$set:{"roles" : "test"}},{"multi":true})
db.getCollection('core_pack').distinct("jobStatus") //distinct字段
//聚合查询
db.getCollection('file_folder').aggregate
([
{
$group:{_id:"$diskType",totalsize:{$sum:"$fileSize"}}
}
])
db.getCollection('file_folder').aggregate
([
{
$match:{
"diskType":{$not:{$eq:"INDIVIDUAL"}}
}
},
{
$group:{_id:"",totalsize:{$sum:"$fileSize"}}
}
])
//最大值
db.getCollection('core_homepage').aggregate([
{
$group:{
_id:"any",
maxSortNo:{
$max:"$sortNo"
}
}
}
])
db.getCollection('core_pack').find({"_id":ObjectId("60740b2287c4b218c82c6f79")}).sort({createdDate: -1}) //降序排序
db.getCollection('core_appversion').find({"visibleEnterprises": {$regex : ".*6007f6b0e4b049831c0d3b3c.*"}}) //正则模糊查询
db.getCollection('core_enterprise').find({"fullName": /辽宁/}) //模糊查询
db.getCollection('core_pack').find({}).sort({createdDate: -1})
db.getCollection('core_pack').find({"_id" :{"$in":[ObjectId("607528d287c4b2212c9c1472"),ObjectId("607532b187c4b205f89dc324")]}}).sort({createdDate:-1}) //in的使用
db.getCollection('core_appversion').
find(
{"$and":[
{"$or":[{"visibleEnterprises": {$regex : ".*6007f6b0e4b049831c0d3b3c.*"}},{"visibleEnterprises":null},{"visibleEnterprises":""}]},
{"platform":"iPhone"}
]
}
)//or的使用
db.getCollection('core_pack').update({"_id":ObjectId("60740b2287c4b218c82c6f79")},{$set:{"status":"ENABLED"}}) //修改数据
db.getCollection('core_pack').remove({
"enterprise" : {
"$ref" : "core_enterprise",
"$id" : ObjectId("6007f6b0e4b049831c0d3b3c")
},
"status":"DISABLED",
"$or":[{"jobStatus":"NEW"},{"jobStatus":"RUN"}]
}) //删除数据
//删除多条数据
db.getCollection('file_storage').update({"sharedInside":true},{"$set":{"sharedSet":[]}},{"multi":true})
批量修改字段
var cursor = db.getCollection('core_user').find({jobNumber:{$nin:['001487', null]}}, {username:1,jobNumber:1});
while (cursor.hasNext()) {
r=cursor.next();
printjson(r);
db.getCollection('core_user').update({jobNumber:r.jobNumber},{$set:{username: r.jobNumber}});
}
删除字段(value):
db.getCollection('userinfo').update(
// query
{
},
// update
{
$unset:{'last_time':''}
},
// options
{
"multi" : false, // update only one document
"upsert" : true // insert a new document, if no existing document match the query
}
);
删除字段(key)
db.getCollection('userinfo').update(
// query
{
},
// update
{
$unset:{'last_time':''}
},
false,
true
);
mongoDB删除表中一个字段
使用update命令
update命令
update命令格式:
db.collection.update(criteria,objNew,upsert,multi)
参数说明:
criteria:查询条件
objNew:update对象和一些更新操作符
upsert:如果不存在update的记录,是否插入objNew这个新的文档,true为插入,默认为false,不插入。
multi:默认是false,只更新找到的第一条记录。如果为true,把按条件查询出来的记录全部更新。
//例如要把User表中address字段删除 db.User.update({},{$unset:{'address':''}},false, true)
增加字段:
db.getCollection('userinfo').update(
// query
{
},
// update
{
$set:{last_time:new Date()}
},
// options
{
"multi" : true, // update only one document
"upsert" : false // insert a new document, if no existing document match the query
}
);
创建视图
db.createView(
"v_recycle",
"file_folder",
[
{
$group:{
"_id":"any",
tableA:{
$push:{
_id:"$_id",
type:"folder",
fileName:"$name",
fileSize:"$fileSize",
lastModifiedDate:"$lastModifiedDate",
status:"$status",
username:"$username"
}
}
}
},
{
$lookup:{
from:"file_storage",
pipeline:[
{
$match:{
"status":"DISABLED"
}
},
{
$project:{
_id:"$_id",
type:"file",
fileName:"$fileName",
fileSize:"$fileSize",
lastModifiedDate:"$lastModifiedDate",
status:"$status",
username:"$username"
}
}
],
as:"tableB"
}
},
{
$project:{
_id:0,
allValue:{
$setUnion:["$tableA","$tableB"]
}
}
},
{
$unwind:"$allValue"
},
{
$project:{
viewId:"$allValue._id",
type:"$allValue.type",
lastModifiedDate:"$allValue.lastModifiedDate",
fileName:"$allValue.fileName",
fileSize:"$allValue.fileSize",
status:"$allValue.status",
username:"$allValue.username"
}
},
{
$match:{
"status":"DISABLED"
}
},
{
$sort:{
lastModifiedDate: 1
}
}
]
)
db.createView(
"v_recycle",
"file_folder",
[
{
$match:{
"status":"DISABLED"
}
},
{
$group:{
"_id":"$_id",
tableA:{
$push:{
_id:"$_id",
type:"folder",
fileName:"$name",
lastModifiedDate:"$lastModifiedDate",
status:"$status",
username:"$username"
}
}
}
},
{
$lookup:{
from:"file_storage",
pipeline:[
{
$match:{
"status":"DISABLED"
}
}],
as:"tableB"
}
},
{
$project:{
_id:0,
allValue:{
$setUnion:["$tableA","$tableB"]
}
}
},
{
$unwind:"$allValue"
},
{
$project:{
id:"$allValue._id",
type:"$allValue.type",
lastModifiedDate:"$allValue.lastModifiedDate",
fileName:"$allValue.fileName",
status:"$allValue.status",
username:"$allValue.username"
}
},
{
$sort:{
lastModifiedDate: 1
}
}
]
)
union all
//笛卡尔乘积
db.file_folder.aggregate([
{
$lookup:{
from:"file_storage",
localField:"file_test",//file_folder不存在的字段
foreignField:"file_test",//file_storage不存在的字段
as:"tableB"//file_storage别名
}
},
{
$unwind:{//展开数组
path:"$tableB"
}
},
{
$project:{
_id:1,
type:"folder",
name:1,
storageId:"$tableB._id",
storageType:"file",
storageName:"$tableB.fileName"
}
}
])
//union All
db.file_folder.aggregate([
{
$match:{
"status":"DISABLED"
}
},
{
$group:{
"_id":"$_id",
tableA:{
$push:{
folderId:"$_id",
type:"folder",
createdDate:"$createdDate",
folderName:"$name",
lastModifiedDate:"$lastModifiedDate",
status:"$status"
}
}
}
},
{
$lookup:{
from:"file_storage",
localField:"file_test",
foreignField:"file_test",
as:"tableB"
}
},
{
$project:{
_id:0,
allValue:{
$setUnion:["$tableA","$tableB"]
}
}
},
{
$unwind:"$allValue"
},
{
$project:{
folderId:"$allValue.folderId",
id:"$allValue._id",
type:"$allValue.type",
lastModifiedDate:"$allValue.lastModifiedDate",
folderName:"$allValue.folderName",
fileName:"$allValue.fileName",
status:"$allValue.status"
}
},
{
$match:{
"status":"DISABLED"
}
},
{
$sort:{
lastModifiedDate: -1
}
}
])
//先条件后union all
db.file_folder.aggregate(
[
{
$match:{
"status":"DISABLED"
}
},
{
$group:{
"_id":"$_id",
tableA:{
$push:{
_id:"$_id",
type:"folder",
fileName:"$name",
lastModifiedDate:"$lastModifiedDate",
status:"$status"
}
}
}
},
{
$lookup:{
from:"file_storage",
pipeline:[
{
$match:{
"status":"DISABLED"
}
}],
as:"tableB"
}
},
{
$project:{
_id:0,
allValue:{
$setUnion:["$tableA","$tableB"]
}
}
},
{
$unwind:"$allValue"
},
{
$project:{
id:"$allValue._id",
type:"$allValue.type",
lastModifiedDate:"$allValue.lastModifiedDate",
fileName:"$allValue.fileName",
status:"$allValue.status"
}
}
]
)
db.file_folder.aggregate(
[
{
$group:{
"_id":"any", //去重
tableA:{
$push:{
_id:"$_id",
type:"folder",
fileName:"$name",
lastModifiedDate:"$lastModifiedDate",
status:"$status",
username:"$username"
}
}
}
},
{
$lookup:{
from:"file_storage",
pipeline:[
{
$match:{
"status":"DISABLED"
}
},
{
$project:{
_id:"$_id",
type:"file",
fileName:"$fileName",
lastModifiedDate:"$lastModifiedDate",
status:"$status",
username:"$username"
}
}
],
as:"tableB"
}
},
{
$project:{
_id:0,
allValue:{
$setUnion:["$tableA","$tableB"]
}
}
},
{
$unwind:"$allValue"
},
{
$project:{
id:"$allValue._id",
type:"$allValue.type",
lastModifiedDate:"$allValue.lastModifiedDate",
fileName:"$allValue.fileName",
status:"$allValue.status",
username:"$allValue.username"
}
},
{
$match:{
"status":"DISABLED"
}
},
{
$sort:{
lastModifiedDate: 1
}
}
]
)
mongodump备份
./mongodump -h localhost:端口 -uroot -p '密码' -o /hjback/20210805
mongorestore恢复
mongorestore -h 127.0.0.1 --port 27017 -u root -p '密码' /databack/mongo-test/
mongoexport、mongoimport 备份、恢复集合
./mongoexport --host localhost:27017 -u root -p --authenticationDatabase admin -d hyjt_menhu -c core_user -o /data/mongodb_data/hyjt_menhu/bak/20220719.json
./mongoimport --host localhost:27017 -u root -p --authenticationDatabase admin -d hyxx_menhu -c core_user --drop --type=json --file /data/mongodb/bak/20220719.json
mongo权限
mongodb用户权限管理最全攻略:用户的创建、查看、删除与修改,mongodb入坑之旅..._weixin_34332905的博客-CSDN博客