提问人:rodzm 提问时间:8/6/2022 更新时间:8/6/2022 访问量:32
如何从一个对象中获取嵌套值并推送到MongoDB中另一个新创建的对象/文档?
How to get nested values from an object and push to another newly created object/document in MongoDB?
问:
我还在学习 NodeJs 和 MongoDB。很抱歉,如果这对某些人来说可能是一个简单的问题。我正在做一个项目,但我被卡住了,不知道该怎么做。
我创建了 2 个集合:“购物车”和“订单”。
在购物车文档中,我有这个(示例):
/* 1 */
{
"_id" : "11111",
"userId" : "88888",
"productId" : "oranges",
"quantity" : 10,
}
/* 2 */
{
"_id" : "22222",
"userId" : "88888",
"productId" : "apples",
"quantity" : 5,
}
/* 3 */
{
"_id" : "33333",
"userId" : "77777",
"productId" : "grapes",
"quantity" : 5,
}
在订单集合中,我有这个:
/* 1 */
{
"userId": "88888",
"products": [{
"productId": xxxxxx ,
"quantity": xx
}],
"totalAmount": xx
}
现在,我曾经看到所有用户ID 为8888的“购物车”。它给了我一个装有所有这些手推车的物品。但是,如何将产品 ID 和相应的数量推送到带有“userId”:“88888” 的单个订单文档?我需要能够在 mongodb /nodejs 中创建一个函数来创建一个新的订单对象。我希望能够得到这样的东西:Cart.find({userId: 88888})
/* 1 */
{
"userId": "88888",
"products": [
{
"productId": "oranges",
"quantity": 10
},
{
"productId": "apples",
"quantity": 5
},
],
"totalAmount": xx
}
谢谢。
答: 暂无答案
评论
Cart.find({userId: 88888})
totalAmount