Thursday, 11 October 2018

Is it possible to do a query in mongodb (mongoose) that would return results in this way...

I'm using mongoose, but the solution can be in vanilla mongodb as well.Let's say I have a store that has the data formatted something like this:​{ store: "Store A", items: ["cookies", "bananas", "coffee"] }, { store: "Store B", items: ["apples", "bananas", "coffee"] }, { store: "Store C", items: ["cookies", "chocolate"] } ​Now let's say I want to know if stores have "cookies" (I can actually query any kind of item). I'd like to know if it's possible to do a query that would return results in this format:​{ store: "Store A", items: ["cookies", "bananas", "coffee"], hasWhatYouWant: true }, { store: "Store B", items: ["apples", "bananas", "coffee"], hasWhatYouWant: false }, { store: "Store C", items: ["cookies", "chocolate"], hasWhatYouWant: true } So the idea is that I always get the basic structure but I dynamically add a "hasWhatYouWant" that is either true or false if a searched item is in items.

Submitted October 11, 2018 at 08:03PM by JavascriptFanboy

No comments:

Post a Comment