![](/img/trans.png)
[英]Reduce array to objects with nested keys for passing a search query to Model.find in Mongoose
[英]mongoose - get a nested array of objects from Model.find() and manipulate the inner array
我試圖從我的服務器路由內的數據庫中獲取具有特定 ID 的對象數組,然后在數組中添加更改對象的屬性(而不是將 objectID 返回給我的客戶端,我想返回文檔作為具有特定 ID 的對象)。
這是我的代碼:
let orders = await Order.find({restaurant: restaurantID, status: 'PROCESSING'})
for(let order of orders){ //iterate through all orders
for(let element of order.dishes){ //iterate through order.dishes array (called 'element' since the array contains objects)
let dish = await Dish.findOne({_id: element._id})
element['dish'] = dish //create new property for the dish object
delete element._id //remove the ID property since it already exists inside the element.dish object
}
}
orders
中的每個order
對象都包含一個名為dishes
的數組,該數組包含具有屬性amount
和id
。 由於我的前端無法對 ID 做太多事情,所以我想刪除 id 屬性並添加一個名為 disc 的新屬性,該屬性包含 id 指向的菜餚對象。
我面臨的問題是我不知道如何操作orders
數組內容。 每當我將orders
解析為 JSON 並將其發送到我的響應中時,我就會收到一個訂單對象的 JSON 數組,就像我想要的那樣。 但是,當我添加上面粘貼的代碼時,它不會更改我的訂單中的任何內容。
每當我在 for 循環中記錄元素時,它看起來像這樣: EmbeddedDocument {__parentArray: Proxy, __index: 0, $__parent: model, $__: InternalCache, $isNew: false, …}
但是當我將它解析為 JSON 我收到正是我想要的,即: {"amount":1,"_id":"6183b84fec1c3e109a2271be"}
在這種情況下, orders
甚至是一個數組嗎? 如果不是,那么操作它/將文檔作為數組獲取的最簡單方法是什么?
這是我在調試窗口中查看訂單時的樣子:
price (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
price (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
restaurant (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
restaurant (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
status (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
status (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
timestamp (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
timestamp (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
user (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
user (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
__v (get):ƒ () {\n return this[getSymbol].call(this.$__[scopeSymbol] || this, path);\n }
__v (set):ƒ (v) {\n this.$set.call(this.$__[scopeSymbol] || this, path, v);\n }
__proto__:Model
length:1
在郵遞員中, res.body 看起來像這樣(幾乎完全像它應該的那樣,但它只包含 id 而不是菜餚對象):
[
{
"_id": "6183b84fec1c3e109a2271bd",
"user": "6166bc426181646198fc483c",
"restaurant": "6176947ce8b10986b018930e",
"dishes": [
{
"amount": 1,
"_id": "6183b84fec1c3e109a2271be"
},
{
"amount": 2,
"_id": "6183b84fec1c3e109a2271bf"
}
],
"price": 30,
"status": "PROCESSING",
"timestamp": "2021-11-04T10:39:11.800Z",
"__v": 0
}
]
如果這已經解決了,請給我發送問題/答案的鏈接。 我花了最后幾個小時試圖解決這個問題,我已經看了很多問題,但沒有一個答案對我有幫助
這是一個常見的問題。 人們常常感到不安,因為他們無法更改來自 MongoDB 的數據。
問題是,貓鼬有點奇怪; 它不返回簡單的 JSON,而是構造並返回 Mongoose 對象的集合。 這些對象有額外的方法,比如.save()
,但最重要的是它們的嵌套對象是不可變的。 簡單地改變它們的屬性是沒有效果的。
為了修改Mongoose返回的數據,你有兩種可能:
.markModified()
這是一個非常奇怪的概念,但是您所做的更改在您將它們標記為已修改之前不會應用。 例如 :
element['dish'] = dish;
order.save();
這不會有任何影響。 Mongo 中的名稱將保持不變。 你需要.markModified()
:
element['dish'] = dish;
order.markModified("dishes");
order.save(); // Works now :|
您可以使用.lean()
阻止 Mongoose 構建自己的對象。 這將返回可以隨意篡改的簡單 JSON 數據。
let orders = await Order
.find({restaurant: restaurantID, status: 'PROCESSING'})
.lean()
.exec(); // Also add .exec(), it returns a true Promise and not a thenable
獎勵: lean()
更快更輕。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.