简体   繁体   English

如何在数组中查找作为架构中子元素的元素

[英]How to find an element in array which is a subelement in the Schema

likes: {
        count:{type: Number, default: 0},
        authors:{type: [String], default: []}
    }

This is schema for the likes section.这是喜欢部分的架构。 And I want to look if the given username is in the authors array or not?我想看看给定的用户名是否在作者数组中? How to create query for this situation?如何为这种情况创建查询?

await Entry.findOne({ 'likes.authors': { $in: user.username },_id: entryId } )

I have created a query like this but I couldn't get the data while I have a data which has entryId and contains user.username inside of 'likes.authors' .我已经创建了一个这样的查询,但是当我有一个数据具有entryId并且在'likes.authors'中包含user.username时,我无法获取数据。

try this:尝试这个:

Entry.findOne({'likes.authors': {$elemMatch: {type: user.username}},_id:entryId});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM