简体   繁体   中英

Returned object with nested values that comply condition with Mongoose

I'm querying an object from a collection which has a key with an array of object. I want it to return the object and exclude the elements in the array that have "available: false".

Ex.: Object

name: "Willy's Store",
city: "Baigorria",
storeId:"666",
open: [
{day: monday, available: true}, 
{day: tuesday, available:true}, 
{day:wensday, available: true},
{day:thursday, available:false}, 
{day:friday, available:false}
]

on query the expected result should be

name: "Willy's Store",
city: "Baigorria",
storeId:"666",
open: [
{day: monday, available: true}, 
{day: tuesday, available:true}, 
{day:wensday, available: true},
]

I want to only use mongoose tools to achieve it

You can use filter concept of arrays on open object to filter out available days.

 var data = { "name": "Willy's Store", "open": [{ "day": "monday", available: true }, { "day": "tuesday", available: true }, { "day": "wensday", available: true }, { "day": "thursday", available: false }, { "day": "friday", available: false } ] } data.open = data.open.filter(subData => subData.available) console.log(data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM