2016-04-03 57 views
1

我的文件看起來是這樣的:從MongoDB中的數組中移除一個原始元素?

{ 
    _id: "57008339e7537f033b67cf84", 
    title: "my title", 
    urls: ["1.html", "2.html", "3.html"] 
} 

我想從urls刪除"2.html"。引用this question我曾嘗試下面的代碼:

db.collection("bookmarks").update({_id: "57008339e7537f033b67cf84"}, {$pull: {"urls": {$eq: "2.html"}}}, (err, result) => { 
    if (err) { 
     console.log(err); 
    } 
}); 

但輸出是:

{ [MongoError: unknown top level operator: $eq] 
    name: 'MongoError', 
    message: 'unknown top level operator: $eq', 
    driver: true, 
    index: 0, 
    code: 2, 
    errmsg: 'unknown top level operator: $eq' } 

回答

0

你不需要$eq操作:

只需使用{$pull: {"urls": "2.html"}}

db.collection("bookmarks").update({_id: "57008339e7537f033b67cf84"}, {$pull: {"urls": "2.html"}}, (err, result) => { 
    if (err) { 
     console.log(err); 
    } 
}); 

$pull文檔

相關問題