mongodb在数组中查找对象

发布于 2025-01-20 23:20:46 字数 1369 浏览 0 评论 0原文

有人可以帮助我找到一个解决方案以在MongoDB文档中找到一个数组对象,我尝试了几种方法,但仍然无法获得我需要的对象,我正在尝试根据用户数据EG查找来查询使用用户电子邮件和名称,这是我想在文档中找到数组的文档。

{
"_id": "lfe78vlwncr",
"images": [],
"location": [28.1339088, -26.2098077],
"_p_category": "Category$ZjfUGGzbNb",

"image": "5d4663900459815806965d8af5392190_image.jpg",
"imageName": "5d4663900459815806965d8af5392190_image.jpg",
"imageThumb": "5d4663900459815806965d8af5392190_image.jpg",
"isHead": "1",
"storeNumber": null,
"twitter": null,
"superUser": null,
"youtube": null,

"user": {
    "email": "[email protected]",
    "name": "john"
},
"_created_at": "2022-03-15T08:17:46.769Z",

"status": "Approved",
"_wperm": ["*"],
"_rperm": ["*"],
"_acl": {
    "w": "true",
    "r": "true"
},
"bIsSales": "true",
"_updated_at": "2022-03-17T05:06:38.425Z",
"bShow": true,

"imageFour": "49217bd1ccf227f010562be0b9e0b889_image.jpg",
"imageThree": "998866aa6707b520f8a6d214351b6256_image.jpg",
"imageTwo": "a394516d65948af07033ac8812d225b8_image.jpg",

}

这是我到目前为止尝试的方法,但仍无法使用。

db.collection("Place")
.find({

    user: {
        email: req.body.userEmail,
        name: req.body.userName
    }
})
.toArray(function(err, result) {
    if (err) throw err;

    res.json(result);
});

Can someone help me with a solution to find an array object inside the MongoDB document, I've tried a couple of methods but am still not able to get the object that I need I'm trying to query based on the user data e.g finding using user email and name, here is the document that I want to find array in the document.

{
"_id": "lfe78vlwncr",
"images": [],
"location": [28.1339088, -26.2098077],
"_p_category": "Category$ZjfUGGzbNb",

"image": "5d4663900459815806965d8af5392190_image.jpg",
"imageName": "5d4663900459815806965d8af5392190_image.jpg",
"imageThumb": "5d4663900459815806965d8af5392190_image.jpg",
"isHead": "1",
"storeNumber": null,
"twitter": null,
"superUser": null,
"youtube": null,

"user": {
    "email": "[email protected]",
    "name": "john"
},
"_created_at": "2022-03-15T08:17:46.769Z",

"status": "Approved",
"_wperm": ["*"],
"_rperm": ["*"],
"_acl": {
    "w": "true",
    "r": "true"
},
"bIsSales": "true",
"_updated_at": "2022-03-17T05:06:38.425Z",
"bShow": true,

"imageFour": "49217bd1ccf227f010562be0b9e0b889_image.jpg",
"imageThree": "998866aa6707b520f8a6d214351b6256_image.jpg",
"imageTwo": "a394516d65948af07033ac8812d225b8_image.jpg",

}

here is what I tried so far yet still not working.

db.collection("Place")
.find({

    user: {
        email: req.body.userEmail,
        name: req.body.userName
    }
})
.toArray(function(err, result) {
    if (err) throw err;

    res.json(result);
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

衣神在巴黎 2025-01-27 23:20:46

尝试这样的尝试:

db.collection("Place")
.find({
  'user.email': req.body.userEmail,
  'user.name': req.body.userName
})
.toArray(function(err, result) {
    if (err) throw err;

    res.json(result);
});

Playground

Try it like this:

db.collection("Place")
.find({
  'user.email': req.body.userEmail,
  'user.name': req.body.userName
})
.toArray(function(err, result) {
    if (err) throw err;

    res.json(result);
});

Playground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文