use
db.dropDatabase()
db.copyDatabase("srcDbName","destdbName")
首先需要下载安装MongoDB Database Tools(下载MongoDB时不自带,需要额外下载)
方案一:mongodump搭配mongorestore
1.登陆源服务器
2.mongodump -h -u <源数据库用户名> -p <源数据库密码> -d <源数据库名> -o <源服务器上储存数据文件的目标路径>
3.将dump文件传输到目标服务器
4.登陆目标服务器
5.mongorestore -h -u <目标数据库用户名> -p <目标数据库密码> -d <目标数据库名> <目标服务器上储存数据文件的路径>
ps:如果提示报错:error connecting to host: could not connect to server: connection() : auth error: sasl conversation error: unable to authenticate using mechanism "SCRAM-SHA-1": (AuthenticationFailed) Authentication failed.
在-u前面加上一句:--authenticationDatabase admin
方案二:mongoexport搭配mongoimport
use
db..find().forEach(function(d){
db.getSiblingDB("")[""].insert(d);
})
db..drop()
//假设有两个集合user和user_detail,user表的_id对应user_detail表的userId,下面查询name为hhh的全部信息
db.user.aggregate([{
$lookup: {
from: "user_detail",
localField: "_id",
foreignField: "userId",
as: "detail"
}
}, {
$match: {
"name": "hhh"
}
}])
//查询结果
{
"_id":ObjectId("xxx"),
"name":"hhh",
"detail":[{
"userId":ObjectId("xxx"),
"address":"xxxxx"
}]
}
db..aggregate([
{
$lookup:
{
from: "",
localField: "",
foreignField: "",
as: "A_join_B"
}
},
{
$match: { "A_join_B": [] } // 在A表但不在B表,则联表结果为空
},
{
$project: {
"A_join_B": 0,
}
}
])
//但这种方法查出来的staff是指定bg下的所有staff,并无法区分是哪个bu的staff,全混一块了,没有分级展示= =
db.bg.aggregate([
{
$match: {
"_id": ObjectId("xxx")
}
},
{
$lookup: {
from: "bu",
localField: "_id",
foreignField: "bgId",
as: "bg_bu"
}
},
{
$addFields: {
"buId": {
$map: {
input: "$bg_bu",
in: {
input: "$$this._id",
}
}
},
}
},
{
$lookup: {
from: "staff",
localField: "buId",
foreignField: "buId",
as: "bu_staff"
}
}
])
mr = db.runCommand({
"mapreduce" : "",
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; },
"out": "" + "_keys"
})
db[mr.result].distinct("_id")
db..update({}, {$rename:{"旧键名称":"新键名称"}}, false, true)
db..update({},{"$unset":{"":""}},{multi:true})
db..update({查询条件},{$set:{"<字段名>":"<字段值>"}})
db..distinct("")
//查询内容
db..distinct("<要去重的字段名>",<查询条件>)
//查询计数
db..distinct("<要去重的字段名>",<查询条件>).length
//查询内容
db..aggregate([{
$group:{
_id: {name: "$name", sex: "$sex"},
}
}])
//查询计数
db..aggregate([{
$group:{
_id: {name: "$name", sex: "$sex"},
}
},{
$count:"cnt"
}])
//查询id为1或者name为hhh的记录
db..find({$or:[{"name":"hhh"},{"id":1}]})
//查询id为1或2的记录
db..find({"id":{$in:[1,2]}})
mongorestore -h -u <生产数据库用户名> -p <生产数据库密码> -d <生产数据库名> <生产服务器上储存数据文件的路径>
db..update({<查询条件>},{$addToSet:{"":""}})
假设有集合test:[{
"tags":["abc","def"],
"name":"hhh",
},{
"tags":["abc","def","ghi","jkl"],
"name":"ggg",
}]
查询tags精确匹配["abc","def"]的文档:
db.test.find({"tags":["abc","def"]})
返回[{
"tags":["abc","def"],
"name":"hhh",
}]
查询tags包含["abc","def"]的文档:
db.test.find({"tags":{$all:["abc","def"]}})
返回[{
"tags":["abc","def"],
"name":"hhh",
},{
"tags":["abc","def","ghi","jkl"],
"name":"ggg",
}]
查询tags包含"abc"的文档:
db.test.find({"tags":"abc"})
返回[{
"tags":["abc","def"],
"name":"hhh",
},{
"tags":["abc","def","ghi","jkl"],
"name":"ggg",
}]
查询tags包含「包含"g"的元素」的文档:
db.test.find({"tags":{$regex:"g"}})
返回[{
"tags":["abc","def","ghi","jkl"],
"name":"ggg",
}]
假设有集合test:[{
"_id": ObjectId("60e2fe0f824c0000900053e2"),
"ip": "1",
"tags": [{
"k": "k1",
"v": "v1"
}, {
"v": "v2",
"k": "k2"
}, {
"k": "k3",
"v": "v3"
}]
}, {
"_id": ObjectId("60e2fe29824c0000900053e3"),
"ip": "2",
"tags": [{
"k": "k1",
"v": "v1"
}, {
"k": "k2",
"v": "v2"
}, {
"k": "k3",
"v": "v3"
}]
}]
(两个文档的区别:k2和v2的顺序)
Go代码一:
cur, err: = cl.Find(Ctx, bson.M{
"tags": bson.M{
"$all": bson.A{
bson.M{
"k": "k1",
"v": "v1",
},
bson.M{
"k": "k2",
"v": "v2",
},
},
},
})
查询结果一:
{"Ip":"2","Tags":[{"K":"k1","V":"v1"},{"K":"k2","V":"v2"},{"K":"k3","V":"v3"}]}
Go代码二:
cur, err: = cl.Find(Ctx, bson.M{
"tags": bson.M{
"$all": bson.A{
bson.M{
"$elemMatch": bson.M{
"k": "k1",
"v": "v1",
},
},
bson.M{
"$elemMatch": bson.M{
"k": "k2",
"v": "v2",
},
},
},
},
})
查询结果二:
{"Ip":"1","Tags":[{"K":"k1","V":"v1"},{"K":"k2","V":"v2"},{"K":"k3","V":"v3"}]}
{"Ip":"2","Tags":[{"K":"k1","V":"v1"},{"V":"v2","K":"k2"},{"K":"k3","V":"v3"}]}
两次查询的区别:一次认顺序一次不认顺序
理解:elemMatch是匹配元素,不是匹配整个文档,所以文档内的元素顺序不影响匹配结果
db.metric_config.update({
"_id": ObjectId("6386fcbad1209d41709dc263"),
model_type: {
$all: [{
$elemMatch: {
type: "LSTM"
}
}]
}
}, {
$set: {
"model_type.$.status": "succeeded" // $代表获取的下标
}
})
假设有集合test:[{
"user":{
"id":1,
"name":"hhh",
}
}]
查询user name为hhh的文档:
db.find({"user.name":"hhh"})
db..getIndexes()
为单个字段创建索引:
db..ensureIndex({"":1(按索引升序排序)/-1(按索引降序排序))},{"name":<索引名>})
为多个字段创建复合(组合)索引:
db..ensureIndex({"":1/-1(,"":1/-1)...},{"name":<索引名>})
为多个字段创建复合(组合)唯一索引:
db..ensureIndex({"":1/-1(,"":1/-1)...},{"name":<索引名>},{unique:true})
db..dropIndex("")