[英]How to query single document which contains specific value of some element in array of arrays MongoDB
[英]mongodb: transform document structure to set an array into single element
不幸的是,我在某些文件中有錯誤。 一些類別數據存儲為數組。 這是不必要的,因為總是只有一個類別。
因此,如何檢查這種結構的集合中是否存在文檔:
{
"_id" : "q9q2He72qve62mr10",
"category" : [
{
"element" : "Anything"
}
],
"order" : 1
}
...並將其轉換為:
{
"_id" : "q9q2He72qve62mr10",
"category" : "Anything",
"order" : 1
}
這樣行嗎?
var data = [{
"_id": "1",
"category": [{
"element": "In Array"
}],
"order": 1
}, {
"_id": "2",
"category": "Single Item",
"order": 1
}];
console.table(data);
data.forEach(function(v) {
if (typeof v.category === 'object' && typeof v.category.length === 'number') {
v.category = v.category[0].element;
}
});
console.table(data);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.