node.js - MONGODB MULTI PARAMETER SEARCH QUERY -
i have following schema:
var listingschema = new schema({ creatorid : [{ type: schema.types.objectid, ref: 'user' }],//listing creator i.e. specific user roommatepreference: { //preferred things in roommate age: {//age preferences if early20s: { type: boolean, default: true }, late20s: { type: boolean, default: true }, thirtys: { type: boolean, default: true }, fortysandold: { type: boolean, default: true } }, gender: {type:string,default:"male"} }, roominfo: {//your own location of place rent address: {type:string,default:"default"}, city: {type:string,default:"default"}, state: {type:string,default:"default"}, zipcode: {type:number,default:0}, }, location: {//room location type: [number], // [<longitude>, <latitude>] index: '2d' // create geospatial index }, pricing: {//room pricing information monthlyrent: {type:number,default:0}, deposit: {type:number,default:0}, }, availability:{//room availability information durationoflease: { minduration: {type:number,default:0}, maxduration: {type:number,default:0}, }, moveindate: { type: date, default: date.now } }, amneties : [{ type: schema.types.objectid, ref: 'amnety' }], rules : [{ type: schema.types.objectid, ref: 'rule' }], photos : [{ type: schema.types.objectid, ref: 'media' }],//array of photos having photo's ids, photos belong media class description: string,//description of room roomi status:{type:boolean,default:true}//status of entry, default active=true }, { timestamps:true } );
the application background airbnb/roomi app, users can give rooms/places on rent. want implement filter user finding appropriae listing of room.
here creatorid, rules, amneties refids of other schemas. want write query give me listings based on several parameters, e.g. user can pass rules, pricing info, amneties, gender etc in req queries. query parameters depends upon user's will. there way nested query thing this?, way did in sql.
well, mongodb not made used relational db.
instead, suggest transforming amenities array array of objects amenities embeded inside listings schema.
so can query follows:
// schema listschema = mongoose.schema({ .... amneties: [{atype: 'shower'}] // or can make simple array of strings: // amneties: ['shower'] .... }) // query listings.find({'amneties.atype' : <some amenity>})
there no joins in mongodb, can still make "joins" mongoose calls them populate, happening on server, , every populations requires round trip server.
if still wish use references amneties collection, should query first , populate listing object on them.
Comments
Post a Comment