MongoDB查询等各种东西
我们在实际业务中使用MongoDB作为NoRDB和文档数据存储。在实现、调查和迁移中,我们经常创建各种MongoDB查询并进行常用操作。下面是一些基本查询操作的备忘录。
1. 数据结构
データベース db
+ーーコレクション collection
| +ドキュメント document
| | {
| | "_id" : ObjectId("54bc6d910cf29e7da446d9ef"),
| | "変数名" : 値
| | ・
| | ・
| | }
| +ドキュメント document
| | {
| | "_id" : ObjectId("54bc6d910cf29e7da446d9ed"),
| | ・
| | ・
| | }
| ・
| ・
データベース db
+ーーコレクション collection
| +ドキュメント document
| +ドキュメント document
| ・
| ・
2 插入
単数の挿入
データベース db
+ーーコレクション collection
| +ドキュメント document
| | {
| | "_id" : ObjectId("54bc6d910cf29e7da446d9ef"),
| | "変数名" : 値
| | ・
| | ・
| | }
| +ドキュメント document
| | {
| | "_id" : ObjectId("54bc6d910cf29e7da446d9ed"),
| | ・
| | ・
| | }
| ・
| ・
データベース db
+ーーコレクション collection
| +ドキュメント document
| +ドキュメント document
| ・
| ・
- 単数の挿入
可以直接保存json格式的文档。(似乎最大限制为16兆字节/文档)
$> db.color.insert(
{
"name" : "black" ,
"nameJpn" : "黒" ,
"hexCode" : "#000000",
"r" : 0,
"g" : 0,
"b" : 0,
}
)
- 複数の挿入
$> db.color.insert([
{"name" : "black" "nameJpn" : "黒", "hexCode" : "#000000","r" : 0,"g" : 0 ,"b" : 0},
{"name" : "gray" "nameJpn" : "灰", "hexCode" : "#808080","r" : 128,"g" : 128 ,"b" : 128},
{"name" : "silver" "nameJpn" : "銀", "hexCode" : "#C0C0C0","r" : 192,"g" : 192 ,"b" : 192},
{"name" : "white" "nameJpn" : "白", "hexCode" : "#FFFFFF","r" : 255,"g" : 255 ,"b" : 255},
{"name" : "blue" "nameJpn" : "青", "hexCode" : "#0000FF","r" : 0,"g" : 0 ,"b" : 128},
{"name" : "navy" "nameJpn" : "ネイビー", "hexCode" : "#000080","r" : 0,"g" : 0 ,"b" : 128},
{"name" : "teal" "nameJpn" : "かものは", "hexCode" : "#008080","r" : 0,"g" : 128 ,"b" : 128},
{"name" : "green" "nameJpn" : " 緑", "hexCode" : "#008000","r" : 0,"g" : 128 ,"b" : 0},
{"name" : "lime" "nameJpn" : "ライム", "hexCode" : "#00FF00","r" : 0,"g" : 256 ,"b" : 0},
{"name" : "aqua" "nameJpn" : "水", "hexCode" : "#00FFFF","r" : 0,"g" : 256 ,"b" : 256},
{"name" : "yellow" "nameJpn" : "黄", "hexCode" : "#FFFF00","r" : 255,"g" : 256 ,"b" : 0},
{"name" : "red" "nameJpn" : " 赤", "hexCode" : "#FF0000",r : 255,"g" : 0 ,"b" : 0},
{"name" : "fuchsia" "nameJpn" : "フクシア", "hexCode" : "#FF00FF","r" : 255,"g" : 0 ,"b" : 256},
{"name" : "olive" "nameJpn" : "オリーブ", "hexCode" : "#808000","r" : 128,"g" : 128 ,"b" : 0},
{"name" : "purple" "nameJpn" : "紫", "hexCode" : "#800080","r" : 128,"g" : 0 ,"b" : 128},
{"name" : "maroon" "nameJpn" : "マルーン", "hexCode" : "#800000","r" : 128,"g" : 0,"b" : 0}
])
找到
在`find`函数的括号中填写搜索条件,如`db.targetCollection.find({“value”:”999″})`。
阶层表示的值只需一个选项,请用中文对以下内容进行释义:
指定 body 层次结构中的 height 属性。
$> db.helthcheck.find( { “body.height”: 150 } )
指定第三次测量的 body 层次结构中的 height 属性。
$> db.helthcheck.find( { “body.2.height”: 150 } )
在中文中对以下内容进行转述,只需提供一种选择:数值评估(= != < > <= >=)
演算子MongoDB概要サンプルvalue = 値:等しい{ value:1000 }value < 値$lt右辺より小さい{ value:{$lt:1000} }value <= 値$lte右辺以下{ value:{$lte:1000} }value > 値$gt右辺より大きい{ value:{$gt:1000} }value >= 値$gte右辺以上{ value:{$gte:1000} }value != 値$ne等しくない{ value:{$ne:1000} }
条件连结
演算MongoDBAND, つなぎもしくは$andOR$or
$> db.client.find( {"lastName":"太郎","zip":"910-3099"}
$> db.client.find( $or: [{ age: { $lt: 18 } },{ age: { $gte: 65 } }] })
$> db.client.find( {
$and : [
{
$or : [
{"firstName" : "佐藤"},
{"lastName" : "太郎"}
]
},
{
"Phone":"03-1234-5678"
}
]
})
在给定的句子中多个值匹配。在中文中价值一条格式化的句子,只需要一个选项:
$> db.targetCollection.find( { “clientId”: { $in: [ “1234”, “3454”, “4567”] } } )
$> db.targetCollection.find({ “clientId”: { $in: [ “1234”, “3454”, “4567”] } })。
存在与否的价值$> db.targetCollection.find({ “createDate”: { $exists: false } })
$> db.targetCollection.find({ “createDate”: { $exists: false } })
独一无二的在中国人的母语中,将以下内容进行释义,仅需要一种选择:
$> db.targetCollection.distinct(“distinctKey”, “createDate”:{$exists:false}})
$> 在db.targetCollection中以”createDate”:{$exists:false}}作为条件查询,返回”distinctKey”的唯一值。
ObjectId的匹配$> db.targetCollection.find({“_id”: {“$oid”: “34917234623847612984”}}}) 可以表达为
$> db.targetCollection.find({“_id”: ObjectId(“34917234623847612984”)}})。
时间的比较 (Time comparison)提取日期
从目标集合中查找日期范围在东京标准时间+9时区的日期范围内的数据。
文字串匹配(类似)从数据库中提取带有”camera”的商品。
$> db.targetCollection.find({ “name” : /camera/ })
仅提取以”camera”开头的商品。
$> db.targetCollection.find({ “name” : /^camera/ })
达到的数量查询目标集合中”value”字段为”999″的数据数量。
排序1: 升序,-1:降序
$> db.targetCollection.find().sort({‘value1’:1, ‘value2’:-1})
最小值、最大值、平均值、总和
$> db.product.aggregate([
{
$group: {
_id: "$kind",
total: { $sum: "$price" }
}
}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"max": { $max: "$price" },
"min": { $min: "$price" }
}}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"avg": { $avg: "$price" }
}}
])
4 更新
更新即更新一些新的信息或者内容。
更新某一部分内容$> db.client.update( { “Name”: “aoki” }, { $set: { “age”: 67 } },false,false)
update操作的最后两个标志的含义
最后第二个标志…false表示upsert(如果记录不存在,则插入;存在则更新),默认值为false
最后一个标志…….true表示multi(批量更新多个符合条件的记录)
更新全部$> db.client.update({}, { $set: { “targetflag” : “true” } }, false, true)
更新db.client的所有文档,将”targetflag”字段的值设为”true”,只更新一条记录,且允许插入新文档。
更新数组把18岁成年人可以进行选举设为真的一个示例:
$> db.client.update({“clientinfo.year”:{“$gte”:18}},{$set:{“clientinfo.$.senkyo”:”true”}},false,true)
更改字段名称$> db.client.update({}, { $rename : { “oldName” : “newName” } });
【Chinese】$> db.client.update({}, { $rename : { “oldName” : “newName” } });
删除
删除物体请注意,如果没有添加条件,则会删除全部内容。
刪除部分我想删除集合中的一个地方。
$> db.client.update({“Name”: “aoki”}, {$unset: {“age”: 1}})
删除所有特定字段。
$> db.client.update({}, {$unset: {“targetDeletefield”: 1}}, false, true)
删除集合删除数据库中的集合。
删除数据库使用数据库名
删除数据库
6 其他
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
将集合拷贝到新集合
$> db.oldCollection.find().forEach(function (x) {db.newCollection.save(x)})
创建索引$> db.infomation.createIndex( { “createDate”: -1, “userId”: 1, “printType”: 1} )
-1表示按照倒序排列,1表示按照升序排列
如果不指定条件,则删除所有索引。
刪除
$> db.infomation.dropIndex({ “createDate”: -1, “userId”: 1, “printType”: 1})
如果沒有條件指定,將刪除所有索引。
执行计划 (Shí
加上 explain()
$> db.collection.find("clientId":"1735").explain()
{
"cursor" : "BtreeCursor clientId_1_reportType_1",
"isMultiKey" : false,
"n" : 7,
"nscannedObjects" : 7,
"nscanned" : 7,
"nscannedObjectsAllPlans" : 7,
"nscannedAllPlans" : 7,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"clientId" : [
[
"1735",
"1735"
]
],
"reportType" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
“cursor” : “BtreeCursor clientId_1_reportType_1″ 可以看出正在使用clientId_1_reportType_1索引
“nscanned” : 7 扫描了7个
“nscannedObjects” : 7 直接读取并检查了7个
項目解説cursorカーソルタイプ。BasicCursor、BtreeCursor、GeoSearchCursorisMultiKeyマルチキーインデックスを使っているかn最終的にヒットしたドキュメント件数nscannedObjectsスキャンされたオブジェクトの数nscannedスキャンされたオブジェクトorインデックスの数nscannedObjectsAllPlansすべてのクエリでスキャンされたオブジェクトの総数nscannedAllPlansすべてのクエリでスキャンされたオブジェクトorインデックスの数scanAndOrderインデックスを使わずに結果をソートして返すかindexOnlyクエリがインデックスのみを使っているか
查询日志调查查询日志级别:
$> db.getProfilingLevel()
或者
$> db.setLogLevel(-1, “query”)
日志级别设置
$> db.setProfilingLevel(2)
或者
$> use admin
$> db.adminCommand({setParameter:1, logLevel:2})
日志级别的种类
对于级别的详细信息不太清楚
Level 解説 その他 Level 0 一番小さいLevel 1
Level 2 Query を確認できる
Level 3
Level 4
Level 5 一番大きい
由于日志文件变得庞大,所以要关心磁盘容量并确保始终可迅速还原。
日志输出
$> db.system.profile.find().pretty()
提取日志:
$> db.system.profile.find({“query.user_id”:10082, “ts”: {“$gt”: ISODate(“2019-11-27T12:13:25.435Z”)}});
以用户和日期为条件进行抽取。
7 简单的JS
db = db.getSiblingDB('test');
print("Start");
var clientCount=0;
var beforeIt = 0;
db.datacollection.find({"dataCreateDate":{$exists: false}}).forEach(function(document) {
var id = document._id;
print("id:" + id);
print("clientId:" + document.clientId);
db.datacollection.update({"_id" : id}, {"$set": { "dataCreateDate" : document.lastUpdateDate}}, false, false);
clientCount++;
});
print(" End count=" + clientCount);
将lastUpdateDate放入没有dataCreateDate的地方进行尝试。
开始和结束mongoDB
$> mongod --dbpath ~/mongoDB_work/data/ --logpath ~/mongoDB_work/mongodb.log
$> use admin
$> db.shutdownServer();
$> quit();
最后我记下了在MongoDB上所做的各种事情。希望对您有所帮助。
条件连结
演算MongoDBAND, つなぎもしくは$andOR$or
$> db.client.find( {"lastName":"太郎","zip":"910-3099"}
$> db.client.find( $or: [{ age: { $lt: 18 } },{ age: { $gte: 65 } }] })
$> db.client.find( {
$and : [
{
$or : [
{"firstName" : "佐藤"},
{"lastName" : "太郎"}
]
},
{
"Phone":"03-1234-5678"
}
]
})
在给定的句子中多个值匹配。在中文中价值一条格式化的句子,只需要一个选项:
$> db.targetCollection.find( { “clientId”: { $in: [ “1234”, “3454”, “4567”] } } )
$> db.targetCollection.find({ “clientId”: { $in: [ “1234”, “3454”, “4567”] } })。
存在与否的价值$> db.targetCollection.find({ “createDate”: { $exists: false } })
$> db.targetCollection.find({ “createDate”: { $exists: false } })
独一无二的在中国人的母语中,将以下内容进行释义,仅需要一种选择:
$> db.targetCollection.distinct(“distinctKey”, “createDate”:{$exists:false}})
$> 在db.targetCollection中以”createDate”:{$exists:false}}作为条件查询,返回”distinctKey”的唯一值。
ObjectId的匹配$> db.targetCollection.find({“_id”: {“$oid”: “34917234623847612984”}}}) 可以表达为
$> db.targetCollection.find({“_id”: ObjectId(“34917234623847612984”)}})。
时间的比较 (Time comparison)提取日期
从目标集合中查找日期范围在东京标准时间+9时区的日期范围内的数据。
文字串匹配(类似)从数据库中提取带有”camera”的商品。
$> db.targetCollection.find({ “name” : /camera/ })
仅提取以”camera”开头的商品。
$> db.targetCollection.find({ “name” : /^camera/ })
达到的数量查询目标集合中”value”字段为”999″的数据数量。
排序1: 升序,-1:降序
$> db.targetCollection.find().sort({‘value1’:1, ‘value2’:-1})
最小值、最大值、平均值、总和
$> db.product.aggregate([
{
$group: {
_id: "$kind",
total: { $sum: "$price" }
}
}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"max": { $max: "$price" },
"min": { $min: "$price" }
}}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"avg": { $avg: "$price" }
}}
])
4 更新
更新即更新一些新的信息或者内容。
更新某一部分内容$> db.client.update( { “Name”: “aoki” }, { $set: { “age”: 67 } },false,false)
update操作的最后两个标志的含义
最后第二个标志…false表示upsert(如果记录不存在,则插入;存在则更新),默认值为false
最后一个标志…….true表示multi(批量更新多个符合条件的记录)
更新全部$> db.client.update({}, { $set: { “targetflag” : “true” } }, false, true)
更新db.client的所有文档,将”targetflag”字段的值设为”true”,只更新一条记录,且允许插入新文档。
更新数组把18岁成年人可以进行选举设为真的一个示例:
$> db.client.update({“clientinfo.year”:{“$gte”:18}},{$set:{“clientinfo.$.senkyo”:”true”}},false,true)
更改字段名称$> db.client.update({}, { $rename : { “oldName” : “newName” } });
【Chinese】$> db.client.update({}, { $rename : { “oldName” : “newName” } });
删除
删除物体请注意,如果没有添加条件,则会删除全部内容。
刪除部分我想删除集合中的一个地方。
$> db.client.update({“Name”: “aoki”}, {$unset: {“age”: 1}})
删除所有特定字段。
$> db.client.update({}, {$unset: {“targetDeletefield”: 1}}, false, true)
删除集合删除数据库中的集合。
删除数据库使用数据库名
删除数据库
6 其他
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
将集合拷贝到新集合
$> db.oldCollection.find().forEach(function (x) {db.newCollection.save(x)})
创建索引$> db.infomation.createIndex( { “createDate”: -1, “userId”: 1, “printType”: 1} )
-1表示按照倒序排列,1表示按照升序排列
如果不指定条件,则删除所有索引。
刪除
$> db.infomation.dropIndex({ “createDate”: -1, “userId”: 1, “printType”: 1})
如果沒有條件指定,將刪除所有索引。
执行计划 (Shí
加上 explain()
$> db.collection.find("clientId":"1735").explain()
{
"cursor" : "BtreeCursor clientId_1_reportType_1",
"isMultiKey" : false,
"n" : 7,
"nscannedObjects" : 7,
"nscanned" : 7,
"nscannedObjectsAllPlans" : 7,
"nscannedAllPlans" : 7,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"clientId" : [
[
"1735",
"1735"
]
],
"reportType" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
“cursor” : “BtreeCursor clientId_1_reportType_1″ 可以看出正在使用clientId_1_reportType_1索引
“nscanned” : 7 扫描了7个
“nscannedObjects” : 7 直接读取并检查了7个
項目解説cursorカーソルタイプ。BasicCursor、BtreeCursor、GeoSearchCursorisMultiKeyマルチキーインデックスを使っているかn最終的にヒットしたドキュメント件数nscannedObjectsスキャンされたオブジェクトの数nscannedスキャンされたオブジェクトorインデックスの数nscannedObjectsAllPlansすべてのクエリでスキャンされたオブジェクトの総数nscannedAllPlansすべてのクエリでスキャンされたオブジェクトorインデックスの数scanAndOrderインデックスを使わずに結果をソートして返すかindexOnlyクエリがインデックスのみを使っているか
查询日志调查查询日志级别:
$> db.getProfilingLevel()
或者
$> db.setLogLevel(-1, “query”)
日志级别设置
$> db.setProfilingLevel(2)
或者
$> use admin
$> db.adminCommand({setParameter:1, logLevel:2})
日志级别的种类
对于级别的详细信息不太清楚
Level 解説 その他 Level 0 一番小さいLevel 1
Level 2 Query を確認できる
Level 3
Level 4
Level 5 一番大きい
由于日志文件变得庞大,所以要关心磁盘容量并确保始终可迅速还原。
日志输出
$> db.system.profile.find().pretty()
提取日志:
$> db.system.profile.find({“query.user_id”:10082, “ts”: {“$gt”: ISODate(“2019-11-27T12:13:25.435Z”)}});
以用户和日期为条件进行抽取。
7 简单的JS
db = db.getSiblingDB('test');
print("Start");
var clientCount=0;
var beforeIt = 0;
db.datacollection.find({"dataCreateDate":{$exists: false}}).forEach(function(document) {
var id = document._id;
print("id:" + id);
print("clientId:" + document.clientId);
db.datacollection.update({"_id" : id}, {"$set": { "dataCreateDate" : document.lastUpdateDate}}, false, false);
clientCount++;
});
print(" End count=" + clientCount);
将lastUpdateDate放入没有dataCreateDate的地方进行尝试。
开始和结束mongoDB
$> mongod --dbpath ~/mongoDB_work/data/ --logpath ~/mongoDB_work/mongodb.log
$> use admin
$> db.shutdownServer();
$> quit();
最后我记下了在MongoDB上所做的各种事情。希望对您有所帮助。
$> db.client.find( {"lastName":"太郎","zip":"910-3099"}
$> db.client.find( $or: [{ age: { $lt: 18 } },{ age: { $gte: 65 } }] })
$> db.client.find( {
$and : [
{
$or : [
{"firstName" : "佐藤"},
{"lastName" : "太郎"}
]
},
{
"Phone":"03-1234-5678"
}
]
})
在给定的句子中多个值匹配。在中文中价值一条格式化的句子,只需要一个选项:
$> db.targetCollection.find( { “clientId”: { $in: [ “1234”, “3454”, “4567”] } } )
$> db.targetCollection.find({ “clientId”: { $in: [ “1234”, “3454”, “4567”] } })。
存在与否的价值$> db.targetCollection.find({ “createDate”: { $exists: false } })
$> db.targetCollection.find({ “createDate”: { $exists: false } })
独一无二的在中国人的母语中,将以下内容进行释义,仅需要一种选择:
$> db.targetCollection.distinct(“distinctKey”, “createDate”:{$exists:false}})
$> 在db.targetCollection中以”createDate”:{$exists:false}}作为条件查询,返回”distinctKey”的唯一值。
ObjectId的匹配$> db.targetCollection.find({“_id”: {“$oid”: “34917234623847612984”}}}) 可以表达为
$> db.targetCollection.find({“_id”: ObjectId(“34917234623847612984”)}})。
时间的比较 (Time comparison)提取日期
从目标集合中查找日期范围在东京标准时间+9时区的日期范围内的数据。
文字串匹配(类似)从数据库中提取带有”camera”的商品。
$> db.targetCollection.find({ “name” : /camera/ })
仅提取以”camera”开头的商品。
$> db.targetCollection.find({ “name” : /^camera/ })
达到的数量查询目标集合中”value”字段为”999″的数据数量。
排序1: 升序,-1:降序
$> db.targetCollection.find().sort({‘value1’:1, ‘value2’:-1})
最小值、最大值、平均值、总和
$> db.product.aggregate([
{
$group: {
_id: "$kind",
total: { $sum: "$price" }
}
}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"max": { $max: "$price" },
"min": { $min: "$price" }
}}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"avg": { $avg: "$price" }
}}
])
4 更新
更新即更新一些新的信息或者内容。
更新某一部分内容$> db.client.update( { “Name”: “aoki” }, { $set: { “age”: 67 } },false,false)
update操作的最后两个标志的含义
最后第二个标志…false表示upsert(如果记录不存在,则插入;存在则更新),默认值为false
最后一个标志…….true表示multi(批量更新多个符合条件的记录)
更新全部$> db.client.update({}, { $set: { “targetflag” : “true” } }, false, true)
$> db.targetCollection.find().sort({‘value1’:1, ‘value2’:-1})
最小值、最大值、平均值、总和
$> db.product.aggregate([
{
$group: {
_id: "$kind",
total: { $sum: "$price" }
}
}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"max": { $max: "$price" },
"min": { $min: "$price" }
}}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"avg": { $avg: "$price" }
}}
])
4 更新
更新即更新一些新的信息或者内容。
更新某一部分内容$> db.client.update( { “Name”: “aoki” }, { $set: { “age”: 67 } },false,false)
update操作的最后两个标志的含义
最后第二个标志…false表示upsert(如果记录不存在,则插入;存在则更新),默认值为false
最后一个标志…….true表示multi(批量更新多个符合条件的记录)
更新全部$> db.client.update({}, { $set: { “targetflag” : “true” } }, false, true)
$> db.product.aggregate([
{
$group: {
_id: "$kind",
total: { $sum: "$price" }
}
}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"max": { $max: "$price" },
"min": { $min: "$price" }
}}
])
$> db.product.aggregate([
{ "$group": {
"_id": null,
"avg": { $avg: "$price" }
}}
])
更新即更新一些新的信息或者内容。
更新某一部分内容$> db.client.update( { “Name”: “aoki” }, { $set: { “age”: 67 } },false,false)
update操作的最后两个标志的含义
最后第二个标志…false表示upsert(如果记录不存在,则插入;存在则更新),默认值为false
最后一个标志…….true表示multi(批量更新多个符合条件的记录)
更新全部$> db.client.update({}, { $set: { “targetflag” : “true” } }, false, true)
更新db.client的所有文档,将”targetflag”字段的值设为”true”,只更新一条记录,且允许插入新文档。
更新数组把18岁成年人可以进行选举设为真的一个示例:
$> db.client.update({“clientinfo.year”:{“$gte”:18}},{$set:{“clientinfo.$.senkyo”:”true”}},false,true)
更改字段名称$> db.client.update({}, { $rename : { “oldName” : “newName” } });
【Chinese】$> db.client.update({}, { $rename : { “oldName” : “newName” } });
删除
删除物体请注意,如果没有添加条件,则会删除全部内容。
刪除部分我想删除集合中的一个地方。
$> db.client.update({“Name”: “aoki”}, {$unset: {“age”: 1}})
删除所有特定字段。
$> db.client.update({}, {$unset: {“targetDeletefield”: 1}}, false, true)
删除集合删除数据库中的集合。
删除数据库使用数据库名
删除数据库
6 其他
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
【Chinese】$> db.client.update({}, { $rename : { “oldName” : “newName” } });
删除
删除物体请注意,如果没有添加条件,则会删除全部内容。
刪除部分我想删除集合中的一个地方。
$> db.client.update({“Name”: “aoki”}, {$unset: {“age”: 1}})
删除所有特定字段。
$> db.client.update({}, {$unset: {“targetDeletefield”: 1}}, false, true)
删除集合删除数据库中的集合。
删除数据库使用数据库名
删除数据库
6 其他
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
刪除部分我想删除集合中的一个地方。
$> db.client.update({“Name”: “aoki”}, {$unset: {“age”: 1}})
删除所有特定字段。
$> db.client.update({}, {$unset: {“targetDeletefield”: 1}}, false, true)
删除集合删除数据库中的集合。
删除数据库使用数据库名
删除数据库
6 其他
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
删除数据库使用数据库名
删除数据库
6 其他
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
更名更改集合名称:db.oldCollection.renameCollection(‘newCollection’)。
请复制数据库复制
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
$> db.copyDatabase(‘源数据库’, ‘目标数据库’)
将集合拷贝到新集合
$> db.oldCollection.find().forEach(function (x) {db.newCollection.save(x)})
创建索引$> db.infomation.createIndex( { “createDate”: -1, “userId”: 1, “printType”: 1} )
-1表示按照倒序排列,1表示按照升序排列
如果不指定条件,则删除所有索引。
刪除
$> db.infomation.dropIndex({ “createDate”: -1, “userId”: 1, “printType”: 1})
如果沒有條件指定,將刪除所有索引。
执行计划 (Shí
加上 explain()
$> db.collection.find("clientId":"1735").explain()
{
"cursor" : "BtreeCursor clientId_1_reportType_1",
"isMultiKey" : false,
"n" : 7,
"nscannedObjects" : 7,
"nscanned" : 7,
"nscannedObjectsAllPlans" : 7,
"nscannedAllPlans" : 7,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"clientId" : [
[
"1735",
"1735"
]
],
"reportType" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
$> db.collection.find("clientId":"1735").explain()
{
"cursor" : "BtreeCursor clientId_1_reportType_1",
"isMultiKey" : false,
"n" : 7,
"nscannedObjects" : 7,
"nscanned" : 7,
"nscannedObjectsAllPlans" : 7,
"nscannedAllPlans" : 7,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"clientId" : [
[
"1735",
"1735"
]
],
"reportType" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
]
}
}
“cursor” : “BtreeCursor clientId_1_reportType_1″ 可以看出正在使用clientId_1_reportType_1索引
“nscanned” : 7 扫描了7个
“nscannedObjects” : 7 直接读取并检查了7个
查询日志调查查询日志级别:
$> db.getProfilingLevel()
或者
$> db.setLogLevel(-1, “query”)
日志级别设置
$> db.setProfilingLevel(2)
或者
$> use admin
$> db.adminCommand({setParameter:1, logLevel:2})
日志级别的种类
对于级别的详细信息不太清楚
Level 2 Query を確認できる
Level 3
Level 4
Level 5 一番大きい
由于日志文件变得庞大,所以要关心磁盘容量并确保始终可迅速还原。
日志输出
$> db.system.profile.find().pretty()
提取日志:
$> db.system.profile.find({“query.user_id”:10082, “ts”: {“$gt”: ISODate(“2019-11-27T12:13:25.435Z”)}});
以用户和日期为条件进行抽取。
7 简单的JS
db = db.getSiblingDB('test');
print("Start");
var clientCount=0;
var beforeIt = 0;
db.datacollection.find({"dataCreateDate":{$exists: false}}).forEach(function(document) {
var id = document._id;
print("id:" + id);
print("clientId:" + document.clientId);
db.datacollection.update({"_id" : id}, {"$set": { "dataCreateDate" : document.lastUpdateDate}}, false, false);
clientCount++;
});
print(" End count=" + clientCount);
db = db.getSiblingDB('test');
print("Start");
var clientCount=0;
var beforeIt = 0;
db.datacollection.find({"dataCreateDate":{$exists: false}}).forEach(function(document) {
var id = document._id;
print("id:" + id);
print("clientId:" + document.clientId);
db.datacollection.update({"_id" : id}, {"$set": { "dataCreateDate" : document.lastUpdateDate}}, false, false);
clientCount++;
});
print(" End count=" + clientCount);
将lastUpdateDate放入没有dataCreateDate的地方进行尝试。
开始和结束mongoDB
$> mongod --dbpath ~/mongoDB_work/data/ --logpath ~/mongoDB_work/mongodb.log
$> use admin
$> db.shutdownServer();
$> quit();
最后我记下了在MongoDB上所做的各种事情。希望对您有所帮助。
$> mongod --dbpath ~/mongoDB_work/data/ --logpath ~/mongoDB_work/mongodb.log
$> use admin
$> db.shutdownServer();
$> quit();