Using Mongoose search a MongoDB Database Collection for a document with matching element in an array
Using Mongoose search a MongoDB Database Collection for a document with matching element in an array
That's it folks. Ask questions in the comments.
Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const personSchema = new Schema({
name : String,
favouriteFoods : [String]
});
module.exports = mongoose.model('Person', personSchema);
Problem
Find a person who has 'Sushi' in its array of favoriteFoods.
Solution
Person.find({ favouriteFoods: "sushi" })
.then(person => console.log(person))
.catch(err => console.error(err));
Comments
Post a Comment