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

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -