node.js - how to query nested arrays in mongoose -
i have schema this
'use strict'; var mongoose = require('mongoose'); var schema = mongoose.schema; var teacherschema = new schema({ education: [{degree: string, institutename: string}], dob: date, photourl: string, phonenumber: string, institutes: [{type: mongoose.schema.objectid, ref: 'institute'}], subjects: [{ name: string, topics: [{ name: string, modules: [{ name: string, classes: [{ name: string, starttime: date, endtime: date, fee: number }] }] }] }], created: {type: date, default: date.now} }) module.exports = mongoose.model('teacher', teacherschema); my question how can query in nested arrays? specific lets want find teachers have @ least 1 subject/topic/module/class name starts "math". how can mongoose?
see if works...
db.teacher.find({$or: [{'subjects':{"$elemmatch":{'name':'math'}}}, {'subjects.topics':{"$elemmatch":{'name':'math'}}}, {'subjects.topics.modules.classes':{"$elemmatch":{'name':'math'}}}] }) however, curious know why modules array needed when contains classes array?
let's want search wildcard "ath"
db.stack30173606.find({$or: [ {'subjects':{"$elemmatch":{'name':'math'}}}, {'subjects.topics':{"$elemmatch":{'name':'math'}}}, {'subjects.topics.modules':{"$elemmatch":{'name':'math'}}}, {'subjects.topics.modules.classes.name':{"$in":[/math/]}} ] }) for case insensitive, check this: how make case-insensitive queries on mongodb?
Comments
Post a Comment