2016-06-17 24 views
2
文件

你好,我有一個初學者的問題,我想...... 我已經環顧四周卻找不到答案MongoDB中刪除不給現場

我有users以下集合在重新定義我的用戶模型之前,我想清理一下 - 我如何刪除所有在以下集合中沒有字段firstname或lastname的文檔?

{ "_id" : ObjectId("57615777a629d16a4c604cce"), "firstName" : "Hector", "lastName" : "yoyo", "email" : "[email protected]", "createDate" : ISODate("2016-06-15T13:26:15.900Z") } 
{ "_id" : ObjectId("57615849a629d16a4c604ccf"), "firstName" : "Arnaud", "lastName" : "yaya", "email" : "[email protected]", "createDate" : ISODate("2016-06-15T13:29:45.517Z") } 
{ "_id" : ObjectId("57615c33a629d16a4c604cd0"), "createDate" : ISODate("2016-06-15T13:46:27.224Z") } 
{ "_id" : ObjectId("57615ff943d8b1a74c4d4216"), "createDate" : ISODate("2016-06-15T14:02:33.352Z") } 

我知道我可以通過一個消除這些之一,但我想有一個更好的解決辦法:)

db.users.remove({ "_id" : ObjectId("57615ff943d8b1a74c4d4216") }); 

感謝

回答

2

使用$exists運營商結合的$or刪除查詢中的操作員:

db.users.remove({ 
    "$or": [ 
     { "firstName": { "$exists": false } }, 
     { "lastName": { "$exists": false } } 
    ] 
}) 
+1

謝謝你的傢伙像一個魅力工作;) –

0

嘗試:

db.users.remove({$或:[{姓:{$存在:假}},{名字:{$存在:假}}]});