[英]Get a single field from a subdocument array in mongodb
Below is my collection schema...以下是我的收藏架构...
What I am trying to do is get the value of "eak" from "Website 1" and use it in my application.我想要做的是从“网站 1”中获取“eak”的值并在我的应用程序中使用它。 I am struggling to pull the value out of the multi embedded array.
我正在努力从多嵌入式数组中提取价值。
_id:1234,
role:"client",
organization:"My Organization",
email:"myorg@notarealemail.com",
password:"myEncryptedPassword"
websites:
[{
_id:5554
name:"Website1"
wak:"567567567"
wid:"5678"
preview:"aSweetImage"
cid:"678"
integrations:
[{
integration_data:[]
_id:9876
name:"Integration 1"
img:"Integration Logo"
},
{
integration_data:[]
_id:8765
name:"Integration 2"
img:"Integration Logo"
eak:"12345678"
}]
},
{
_id:5555
name:"Website2"
wak:"567567568"
wid:"5679"
preview:"aSweetImage"
cid:"679"
integrations:
[{
integration_data:[]
_id:9877
name:"Integration 1"
img:"Integration Logo"
},
{
integration_data:[]
_id:8766
name:"Integration 2"
img:"Integration Logo"
eak:"12345679"
}]
}]
I have access to the "_id" for "Website 1" and am currently trying to call the value like this.我可以访问“网站 1”的“_id” ,目前正在尝试这样调用该值。
Users.find({
eak: { "websites._id": wid, "integrations.name": "Integration 2" },
})
.then((eak) => {
console.log("HERE", eak);
})
.catch((err) => {
console.log(err);
});
I have a feeling that I am simply not fully understanding the way find() works.我有一种感觉,我根本没有完全理解 find() 的工作方式。 I have read the mongodb documentation and I am clearly missing something.
我已经阅读了 mongodb 文档,我显然遗漏了一些东西。
the console is logging:控制台正在记录:
HERE []
There are two problems.有两个问题。
Mongo db us case sensitive. Mongo db 我们区分大小写。 Searching for
website
will not result Website
and Website1
is not same as website 1
搜索
website
不会结果Website
和Website1
1与website 1
不同
If your resolve the above issue, you need to use positional operator to return the matching document.如果您解决了上述问题,您需要使用位置运算符返回匹配的文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.