MongooseJS returning empty array after population - javascript

I'm coding an app using Node.js and MongooseJS as my middleware for handling database calls.
My problem is that I have some nested schemas and one of them is populated in a wrong way. When I track every step of the population - all of the data is fine, except the devices array, which is empty. I double checked the database and there is data inside that array, so it should be fine.
I've got Room schema. Each object of Room has a field called DeviceGroups. This field contains some information and one of them is an array called Devices which stored devices that are assigned to the parent room.
As you can see in the code, I am finding a room based on it's ID that comes in request to the server. Everything is populated fine and data is consistent with the data in the database. Problem is that the devices array is empty.
Is that some kind of a quirk of MongooseJS, or am I doing something wrong here, that devices array is returned empty? I checked in the database itself and there is some data inside it, so the data is fine, the bug is somewhere in the pasted code.
The code:
Schemas:
const roomSchema = Schema({
name: {
type: String,
required: [true, 'Room name not provided']
},
deviceGroups: [{
type: Schema.Types.ObjectId,
ref: 'DeviceGroup'
}]
}, { collection: 'rooms' });
const deviceGroupSchema = Schema({
parentRoomId: {
type: Schema.Types.ObjectId,
ref: 'Room'
},
groupType: {
type: String,
enum: ['LIGHTS', 'BLINDS', 'ALARM_SENSORS', 'WEATHER_SENSORS']
},
devices: [
{
type: Schema.Types.ObjectId,
ref: 'LightBulb'
},
{
type: Schema.Types.ObjectId,
ref: 'Blind'
}
]
}, { collection: 'deviceGroups' });
const lightBulbSchema = Schema({
name: String,
isPoweredOn: Boolean,
currentColor: Number
}, { collection: 'lightBulbs' });
const blindSchema = Schema({
name: String,
goingUp: Boolean,
goingDown: Boolean
}, { collection: 'blinds' });
Database call:
Room
.findOne({ _id: req.params.roomId })
.populate({
path: 'deviceGroups',
populate: {
path: 'devices'
}
})
.lean()
.exec(function(err, room) {
if (err) {
res.send(err);
} else {
room.deviceGroups.map(function(currentDeviceGroup, index) {
if (currentDeviceGroup.groupType === "BLINDS") {
var blinds = room.deviceGroups[index].devices.map(function(currentBlind) {
return {
_id: currentBlind._id,
name: currentBlind.name,
goingUp: currentBlind.goingUp,
goingDown: currentBlind.goingDown
}
});
res.send(blinds);
}
});
}
})

This is an example of using discriminator method to be able to use multiple schemas in a single array.
const roomSchema = Schema({
name: {
type: String,
required: [true, 'Room name not provided']
},
deviceGroups: [{ type: Schema.Types.ObjectId, ref: 'DeviceGroup' }]
});
const deviceGroupSchema = Schema({
parentRoom: { type: Schema.Types.ObjectId, ref: 'Room' },
groupType: {
type: String,
enum: ['LIGHTS', 'BLINDS', 'ALARM_SENSORS', 'WEATHER_SENSORS']
},
devices: [{ type: Schema.Types.ObjectId, ref: 'Device' }]
});
// base schema for all devices
function DeviceSchema() {
Schema.apply(this, arguments);
// add common props for all devices
this.add({
name: String
});
}
util.inherits(DeviceSchema, Schema);
var deviceSchema = new DeviceSchema();
var lightBulbSchema = new DeviceSchema({
// add props specific to lightBulbs
isPoweredOn: Boolean,
currentColor: Number
});
var blindSchema = new DeviceSchema({
// add props specific to blinds
goingUp: Boolean,
goingDown: Boolean
});
var Room = mongoose.model("Room", roomSchema );
var DeviceGroup = mongoose.model("DeviceGroup", deviceGroupSchema );
var Device = mongoose.model("Device", deviceSchema );
var LightBulb = Device.discriminator("LightBulb", lightBulbSchema );
var Blind = Device.discriminator("Blind", blindSchema );
// this should return all devices
Device.find()
// this should return all devices that are LightBulbs
LightBulb.find()
// this should return all devices that are Blinds
Blind.find()
In the collection you will see __t property on each device
with values according to the schema used (LightBulb or Blind)
I haven't tried the code and i haven't used mongoose in a while but i hope it will work :)
Update - tested working example
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var util = require('util');
const roomSchema = Schema({
name: {
type: String,
required: [true, 'Room name not provided']
},
deviceGroups: [{ type: Schema.Types.ObjectId, ref: 'DeviceGroup' }]
});
const deviceGroupSchema = Schema({
parentRoomId: { type: Schema.Types.ObjectId, ref: 'Room' },
groupType: {
type: String,
enum: ['LIGHTS', 'BLINDS', 'ALARM_SENSORS', 'WEATHER_SENSORS']
},
devices: [{ type: Schema.Types.ObjectId, ref: 'Device' }]
});
// base schema for all devices
function DeviceSchema() {
Schema.apply(this, arguments);
// add common props for all devices
this.add({
name: String
});
}
util.inherits(DeviceSchema, Schema);
var deviceSchema = new DeviceSchema();
var lightBulbSchema = new DeviceSchema({
// add props specific to lightBulbs
isPoweredOn: Boolean,
currentColor: Number
});
var blindSchema = new DeviceSchema();
blindSchema.add({
// add props specific to blinds
goingUp: Boolean,
goingDown: Boolean
});
var Room = mongoose.model("Room", roomSchema );
var DeviceGroup = mongoose.model("DeviceGroup", deviceGroupSchema );
var Device = mongoose.model("Device", deviceSchema );
var LightBulb = Device.discriminator("LightBulb", lightBulbSchema );
var Blind = Device.discriminator("Blind", blindSchema );
var conn = mongoose.connect('mongodb://127.0.0.1/test', { useMongoClient: true });
conn.then(function(db){
var room = new Room({
name: 'Kitchen'
});
var devgroup = new DeviceGroup({
parentRoom: room._id,
groupType: 'LIGHTS'
});
var blind = new Blind({
name: 'blind1',
goingUp: false,
goingDown: true
});
blind.save();
var light = new LightBulb({
name: 'light1',
isPoweredOn: false,
currentColor: true
});
light.save();
devgroup.devices.push(blind._id);
devgroup.devices.push(light._id);
devgroup.save();
room.deviceGroups.push(devgroup._id);
room.save(function(err){
console.log(err);
});
// Room
// .find()
// .populate({
// path: 'deviceGroups',
// populate: {
// path: 'devices'
// }
// })
// .then(function(result){
// console.log(JSON.stringify(result, null, 4));
// });
}).catch(function(err){
});

Can you delete everything in you else statement and
console.log(room)
Check your mongo store to make sure you have data in your collection.

Related

how to use find function in nodejs

const categorySchema = mongoose.Schema({
name: {
required: true,
type: String
}
});
const productSchema = mongoose.Schema({
name: {
type: String,
required: true
},
category: {
type: mongoose.Schema.Types.Mixed,
ref: "Category",
required: true
}
});
As you see above I have two models where I am using Category inside Product.
Now I want to fetch all the products by passing a particular Category or its id as _id which gets generated by default by mongodb.
Although I am achieving the desired result do this below:
const id = req.query.categoryId;//getting this from GET REQUEST
let tempProducts = [];
let products = await Product.find({});
products.forEach(product => {
if (product.category._id===id){
tempProducts.push(product);
}
});
It gets me what I want to achieve but still I want to know how to get it using "find" function. or this what I am doing is the only way.
Ok thank you all for your time. I found out the solution which is:
First of all below is my schema as Product which includes another schema named Category:
const productSchema = mongoose.Schema({
name: { type: String, required: true },
category: { type: mongoose.Schema.Types.Mixed, ref: "Category", required: true } });
And to get all the products related to a particular category,
We need to add our query inside quotes like this:
let tempProducts = [];
tempProducts = await Product.find({ "category._id": id});
This is how I achieved as I wanted and working as expected.

Node.js + Mongoose: How to use a virtual property to associate an ObjectID with the property?

I'm trying to access a MongoDB database using Node.js and Mongoose.
I created a virtual property in Schema called username. See the code that follows.
const mongoose = require("mongoose");
const User = require("../models/user");
const datatypes = ['temperature', 'humidity'];
const nodeSchema = new mongoose.Schema(
{
MACAddress: {
type: String,
required: true,
trim: true,
uppercase: true,
match: /^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$/,
},
alias: {
type: String,
trim: true,
},
coordinates: {
type: String,
required: false,
match: /^(\-?\d+(\.\d+)?),\s*(\-?\d+(\.\d+)?)$/,
},
address: {
type: String,
required: false,
},
userID: {
type: mongoose.Types.ObjectId,
},
nodeType: {
type: String,
enum: ['router', 'node'],
default: 'node',
},
dataTypes: {
type: [String],
enum: datatypes,
required: true,
}
},
{
timestamps: true,
}
);
The virtual property is used to set the userID property. See the code that follows.
// virtual field
nodeSchema.virtual("username").set(async function (username) {
this.userID = await this.getUserID(username);
});
// methods
nodeSchema.methods = {
getUserID: function (username) {
if (!username) return null;
return User.find({username: username}).then(userDoc => userDoc[0]._id);
},
};
To add a new document to the database, I am using the following code.
const newNode = new Node(newNodeData);
newNode.save().then( (node) => {
console.log(node.userID)
}
)
The problem is this... Calling the User.find function returns a promise. Even using await (see previous code), newNode.save() saves the document in the database without the userID property.
If I change the code to the following snippet, which doesn't use promise, the userID property is saved in the database with no problem. However, this is not what I want.
// virtual field
nodeSchema.virtual("username").set(async function (username) {
let ObjectId = mongoose.Types.ObjectId;
this.userID = new ObjectId("6245e896afe465a25047302e");
});
How can I force newNode.save() to wait for the promise result before saving the document to the database?

How to populate an array of model instances inside a subdocument? MongoDB Mongoose

I have a subdocument that is nested as an array. Inside that subdocument I have references to other models. Using the .Find and .Populate methods I can receive the entire objects for single models referenced in the subdocument (check out Stop below) but not an array of model instances, Facts/Recommendations. For Facts/Recommendations I receive an array of object _ids. I can probably just take those _ids and make another query but this seems messy.
Is there a way to populate the array? Do I need to use Aggregate + $lookup? Is there a more Mongo way to restructure the Schema to simplify this?
Thank you for all and any help!
My subdocument called TourStop:
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const TourStopSchema = new Schema({
stop: {
type: Schema.Types.ObjectId,
ref: 'Stop',
required:[true, 'must have a Stop'],
},
mandatory: {
type:Boolean,
default: false
},
check_in_time: {
type: Number,
default: 0
},
order: {
type:Number,
required: [true, 'Must have an order']
},
facts: [{
type: Schema.Types.ObjectId,
ref: 'Fact'
}],
recommendations: [{
type: Schema.Types.ObjectId,
ref: 'Recommendation'
}]
});
module.exports = TourStopSchema;
TourStops lives inside of Tour:
const mongoose = require('mongoose');
const TourStopSchema = require('../subdocs/tour_stop');
const Schema = mongoose.Schema;
const tourSchema = new Schema({
name: {
type:String,
required:[true,'Name is required!'],
unique: true
},
city: {
type: String,
required: [true, 'City is required!']
},
tourStops:[TourStopSchema]
});
const Tour = mongoose.model('Tour', tourSchema);
module.exports = Tour;
Stop Schema which is populated just fine.
const mongoose = require('mongoose');
const LocationSchema = require('../subdocs/location');
const ImageSchema = require('../subdocs/image');
const Schema = mongoose.Schema;
const stopSchema = new Schema({
name:{
type: String,
required:[true,'Must have a name!']
},
location:LocationSchema,
categories: {
type: [String],
default:[]
},
images:{
type:[ImageSchema],
default:[]
}
});
const Stop = mongoose.model('Stop', stopSchema);
module.exports = Stop;
And Fact Schema which is not populated correctly, instead returns an array of strings with the _ids
Fact:
const mongoose = require('mongoose');
const ImageSchema = require('../subdocs/image');
const Schema = mongoose.Schema;
const factSchema = new Schema({
stop: {
type: Schema.Types.ObjectId,
ref:'Stop',
required:[true, 'A Fact must have a Stop!'],
},
description: {
type: String,
required: [true,'A Fact must have a description!']
},
images: {
type:[ImageSchema],
default:[]
}
});
const Fact = mongoose.model('Fact', factSchema);
module.exports = Fact;
And I'm running a test to check that the schema is properly hooked up to retrieve all the attributes of a TourStop:
it('saves a full relation graph', (done) => {
User.findOne({ first_name: 'Dean' })
.populate({
// in that user find the tours property and load up all tours
path: 'tours',
// inside of all those tours, find the tourstops property and load all associated stops
populate: {
path: 'tour_stops.facts',
model: 'Fact'
},
populate: {
path: 'tour_stops.stop',
model: 'Stop'
}
})
// .populate('tours.tour_stops[0].facts')
.then((user) => {
// console.log(user.tours[0].tour_stops[0].stop);
console.log(user.tours[0].tour_stops[0]);
// console.log(Array.isArray(user.tours[0].tour_stops[0].facts))
assert(user.first_name === 'Dean' );
assert(user.tours.length === 1);
assert(user.tours[0].name === "Awesome New Tour");
assert(user.tours[0].tour_stops[0].stop.name === 'Jaffa Clock Tower');
// assert(user.tours[0])
// assert(user.blogPosts[0].title === 'JS is Great');
// assert(user.blogPosts[0].comments[0].content === 'Congrats on great post!' );
// assert(user.blogPosts[0].comments[0].user.name === 'Joe' )
done();
})
})
You can use the following code to populate tours, stop, facts and recommendations.
Note in model property, we should not give string value, but the model itself. So you need to import them to your code.
User.findOne({ first_name: "Dean" })
.populate({
path: "tours",
populate: {
path: "tourStops.stop",
model: Stop
}
})
.populate({
path: "tours",
populate: {
path: "tourStops.facts",
model: Fact
}
})
.populate({
path: "tours",
populate: {
path: "tourStops.recommendations",
model: Recommendation
}
})
.then(user => {
console.log(user);
});

How do I find if an Id is present in the array of team members (which stores user ids)?

I have this model of workspace schema in my node js project(model is displayed below)
After the user logs into my application I want to display the information of a workspace only if it is created by him or he is a team member of that workspace
I am able to find the workspaces created by the user by the following query
Workspace.find({creator:req.user._id},function(err,workspaces){
res.render('home',{
wokspacses:workspaces
});
});
similarly, I also want the workspaces in which the user is the team member
Workspace.find({creator:req.user._id},function(err,workspaces){
Workspace.find({team_member:"WHAT SHOULD I WRITE HERE"},function(err,workspaces2){
res.render('home',{
wokspacses:workspaces
wokspacses2:workspaces2
});
});
Since team_members is an array simply passing the user id is not yielding the result and workspaces2 remains empty
Thank you for your time !!
const mongoose = require('mongoose');
const workspaceSchema = mongoose.Schema({
name:{
type:String,
required:true
},
date: {
type: Date,
required: true
},
creator:{
type: Object,
ref: 'User',
required: true
},
team_member: [{ type: Object, ref: 'User' }]
});
module.exports = mongoose.model('Workspace',workspaceSchema);
Use the $in Operator.
const mongoose = require("mongoose")
const Schema = mongoose.Schema
mongoose.connect('mongodb://localhost/stackoverflow', {useNewUrlParser: true});
const workspaceSchema = new Schema({
name:{
type:String,
required:true
},
date: {
type: Date,
required: true
},
creator:{
type: Object,
ref: 'User',
required: true
},
team_member: [{ type: Object, ref: 'User' }]
});
const WorkspaceModel = mongoose.model('Workspace',workspaceSchema);
const sessionUserId = "5d330f3de87ec83f95504c44" //i.e. req.user._id;
WorkspaceModel.find({
$or:[
{ creator: sessionUserId },
{
team_member: {
$in: [sessionUserId]
}
}
]
}).exec((err, result) => {
console.log("result", result)
})

".findOneAndUpdate()" not updating database properly (Mongodb & Node.js)

I try to use .findOneAndUpdate() to update my database.
No error message, but this part of the database is not updated with new data. The embedded document competitorAnalysisTextData is still empty.
// on routes that end in /users/competitorAnalysisTextData
// ----------------------------------------------------
router.route('/users/competitorAnalysisTextData/:userName')
// update the user info (accessed at PUT http://localhost:8080/api/users/competitorAnalysisTextData)
.post(function(req, res) {
console.log('1');
// Just give instruction to mongodb to find document, change it;
// then finally after mongodb is done, return the result/error as callback.
User.findOneAndUpdate(
{ userName : req.params.userName},
{
$set:
{ "competitorAnalysis.firstObservation" : req.body.firstObservation,
"competitorAnalysis.secondObservation" : req.body.secondObservation,
"competitorAnalysis.thirdObservation" : req.body.thirdObservation,
"competitorAnalysis.brandName" : req.body.brandName,
"competitorAnalysis.productCategory" : req.body.productCategory
}
},
{ upsert: true },
function(err, user) {
// after mongodb is done updating, you are receiving the updated file as callback
console.log('2');
// now you can send the error or updated file to client
if (err)
return res.send(err);
return res.json({ message: 'User updated!' });
});
})
Update
This is my "User" Schema part:
// grab the things we need
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// Require the crypto module for password hash
'use strict';
var crypto = require('crypto');
// create competitorAnalysisSchema
var CompetitorAnalysis = new Schema({
firstObservation: { type: String },
secondObservation: { type: String },
thirdObservation: { type: String },
brandName: { type: String },
productCategory: { type: String }
});
// create competitorAnalysisPhotoSchema
var CompetitorAnalysisPhoto = new Schema({
photo1: {type: String},
photo2: {type: String},
photo3: {type: String},
photo4: {type: String}
});
// create UserSchema
var UserSchema = new Schema({
userName: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
password: { type: String, required: true },
currentDemo: { type: String },
nextDemo: { type: String },
startTime: { type: String },
startLocation: { type: String },
arriveTime: { type: String },
arriveLocation: { type: String },
leaveTime: { type: String },
leaveLocation: { type: String },
competitorAnalysis: [CompetitorAnalysis],
competitorAnalysisPhoto: [CompetitorAnalysisPhoto],
created_at: Date,
updated_at: Date
});
// the schema is useless so far
// we need to create a model using it
var User = mongoose.model('User', UserSchema);
// make this available to our users in our Node applications
module.exports = User;
in javascript if you wish to update an object inside an array, you need to pick the index
var arr = [{name: "person1"},{name:"person2"}]
arr[0].name = "myname"
arr[1].name = "myFriend"
So it's the same in mongodb, check this link for detail example, or you can manually input the index, for quick hack.
User.findOneAndUpdate(
{ userName : req.params.userName},
{
$set:
{ "competitorAnalysis.0.firstObservation" : req.body.firstObservation,
"competitorAnalysis.0.secondObservation" : req.body.secondObservation,
"competitorAnalysis.0.thirdObservation" : req.body.thirdObservation,
"competitorAnalysis.0.brandName" : req.body.brandName,
"competitorAnalysis.0.productCategory" : req.body.productCategory
}
},
{ upsert: true },
function(err, user) {
// after mongodb is done updating, you are receiving the updated file as callback
console.log('2');
// now you can send the error or updated file to client
if (err)
return res.send(err);
return res.json({ message: 'User updated!' });
});
})
You should use the code above to update nested-array not to add to empty-array.
In javascript, if an array is still empty, we use .push() to add, while in mongodb the command is $push
var arr = []
arr.push({name:"person1"})

Categories

Resources