Reputation: 1106
In MongoDb I currently have documents that resemble the below one shown. I have multiple documents like the one below. For example, I would like to pull back only the image paths that contain at least 25 percent of hex value BA4D2A and 75 percent of FFAB23.
{
"__v": 0,
"_id": {
"$oid": "51c1ac95f502f20969000022"
},
"hex": "BA4D2A",
"img": [
{
"path": "http://example.com/image1.jpg",
"perc": 10,
"_id": {
"$oid": "51c1ac95f502f20969000023"
}
},
{
"path": "http://example.com/image2.jpg",
"perc": 20,
"_id": {
"$oid": "51c1ac95f502f20969000034"
}
}
]
}
After kindly getting help from a previous question, I have the below query that works awesome for pulling back the paths if I was looking for all hex values having over a single specified percentage amount, but I realized that I would like to be able to handle each percentage seperately for each hex value.
If you couldn't tell I am about 4 days into playing with Mongoose/MongoDb, so if there is a better way of doing this (architecture wise) I am open to ideas. This approach is just something I came up with from reading the docs and google.
clrModel.find({
hex: {$in: [ "5B6D8B", "BA4D2A" ] } ,
'img.perc': {$gte: 5}
} ,
'img.path',
function(error,data){
//Handle my data res accordingly
});
};
EDIT ----------- I have managed to get something a little closer to what I want with the below code block but I want only the paths that meet all the criteria. The below query returns back all the results for each individual hex query, but I am wanting the paths for only the images that contain both hex values and their percentages if that makes sense.
clrModel.find({
$or: [
{hex: "5B6D8B", 'img.perc':{$gte: 5}},
{hex: "BA4D2A", 'img.perc':{$gte: 12}}
],
} ,
'img.path',
function(error,data){ });
Upvotes: 0
Views: 228
Reputation: 88
I think issue is the way you are structuring your entries. There is only one hex field per document, which means that it is impossible for a document to contain both values. To query what you want, each path name needs to have an associated array of hex values and percentages, so you could then execute an $and query that works.
Upvotes: 1