javascript - How to find by array of objects in Mongoose? -
i have mongoose.schema this:
const pixelschema = mongoose.schema({ x: string, y: string, color: string, });
also have array of objects this:
let pixels = [ {x: 0, 1: 0, color: 'blue'}, {x: 0, y: 1, color: 'blue'}, {x: 0, y: 2, color: 'blue'}, ]
how can check 1 of elements exist in database? solution looks this, think it's inefficient.
pixels.map(pixel => { pixel.find(pixel, (err, pixels) => { if (pixels) { console.log('find!'); } }); });
use array part of $or
query document. $or
operator lets perform logical or operation on array of 2 or more expressions , selects documents satisfy @ least 1 of expressions.
so query in end should be:
let pixels = [ {x: 0, y: 0, color: 'blue'}, {x: 0, y: 1, color: 'blue'}, {x: 0, y: 2, color: 'blue'}, ]; let query = { "$or": pixels }; pixel.find(query, (err, pixels) => { if (pixels) { console.log('find!'); } });
Comments
Post a Comment