前言
這是一篇日常使用MongoDB時候遇到的問題的解決技巧的文章,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧。
一、查找數(shù)組字段不為空的記錄
查找數(shù)據(jù)中數(shù)組字段不為空的記錄。
舉個例子:有以下Mongo文檔,
{ "id" : "581c060f2b436c05aafb1632", "commit_history" : [ "581c20d52b436c05aafb1633", "581c21c12b436c05aafb1634" ]},{ "id" : "581c060f2b436c05aafb1633", "commit_history" : []}想要查找commit_history不為空的記錄,有以下方法:
方法一: db.collection.find({commit_history: {$not: {$size: 0}}})
方法二: db.collection.find({'commit_history.0': {$exists: 1}})
二、MongoDB添加用戶
在MongoDB中為一個Collection添加用戶,可以如下操作:
use collection_name 切換到某個庫
db.createUser( { user: "collection_name", pwd: "password", roles: [ "readWrite", "dbAdmin" ] })三、有的時候需要刪除指定字段那一列,使用update操作。
例如要刪除name這一列:
query json:
{"name":{$exists:true}} update json:
{$unset:{"name":""}} 四、數(shù)據(jù)導(dǎo)出,在mongodb的bin目錄執(zhí)行mongoexport命令并設(shè)定相關(guān)的參數(shù)
例如: