简体   繁体   English

使用 mongoose 从数组中返回匹配的文档

[英]return documents that match from an array using mongoose

So basically i have a string of message and i want to get the document that matches the keyword所以基本上我有一串消息,我想获取与关键字匹配的文档

Tag to match: Time, Family标签匹配:时间,家庭

const sample_message = "This user have a challenge of Time and Family"

document_1 = {
challenge: Object ID 1
keywords: [ "Time", "Test" ]
}
document_2 = {
challenge: Object ID 2
keywords: [ "Family", "Test2" ]
}
document_3 = {
challenge: Object ID 3
keywords: [ "Foo", "Test3" ]
}



const result = model.find(keywords: { new Regexp(sample_message) })
console.log(result)

I need to return all the documents that match the keywords from sample_message which is the "Time" and "Family".我需要返回与 sample_message 中的关键字匹配的所有文档,即“时间”和“家庭”。 Can someone explain the correct way?有人可以解释正确的方法吗?

db.model.find(
    {
        'keywords': {
            $elemMatch: {
                $elemMatch: {
                    $in: ['Time', 'Family']
                }
            }
        }
    },
    {
        "values.$": 1
    }
)

暂无
暂无

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

相关问题 使用 mongoose 从 mongodb(非数组)返回值 - Return value from mongodb (not array) using mongoose 如何使用 mongoose 中的用户数组 object id 返回文档数组? - How do I return an array of documents using an array of users object ids in mongoose? 猫鼬:find()-匹配两个值并仅返回匹配的文档 - Mongoose: find() - match two values and return only the documents matched 如何基于子字符串数组上的字符串匹配查找猫鼬文档 - How to find mongoose documents based on a string match on an array of substrings 在 mongoose subdoc 数组中查找以返回具有相似字段的文档数组 - find in array of mongoose subdoc to return array of documents with similar fields 来自不同集合的子文档ID数组的猫鼬模式 - Mongoose schema for array of sub-documents ids from a different collection 如何在 Mongoose 中找到与给定查询值匹配的数组字段的最后一个元素的所有文档? - How can you find all documents that match the last element of an array field with a given query value in Mongoose? 有没有一种方法可以查询 mongoDB/mongoose 中的文档以匹配日期时间大于指定日期的数组属性中的所有对象 - Is there a way to query documents in mongoDB/mongoose to match ALL objects in an array property WHERE the datetime is greater than a specified date 如何使用mongoose计算mongodb集合文档中的字段总和 - How to calculate sum of fields from mongodb collection documents using mongoose 猫鼬查找-从查询中返回至少匹配一个的所有对象 - Mongoose find - return all that match at least one from query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM