简体   繁体   中英

Filter by id in on one to many field

Say I have this userSchema :

const userSchema = new Schema({
    firstName: {type: String, required: true},
    lastName: {type: String, required: true},
    email: {type: String, required: true, unique: true},
});

and this chatSchema :

const chatSchema = new Schema({
    user_ids: {type: [String]},
});

I wonder if I could do a search to get all chats from a user that has their user_id inside chat's user_ids and how could I do that.

If you are referencing the user schema and you just need to query the user_id you can simply do it like this:

 db.chat.aggregate([{$macth:{"user_ids" : user_id}}])

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