简体   繁体   中英

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". Can someone explain the correct way?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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