I try unsuccessfully to accept an array using populate...
my Objects:
var giftSchema = new Schema({
name: String,
prod_id :Number,
price : Number,
interests : [{ type : mongoose.Schema.Types.ObjectId, ref: 'inGiftInterSchema'}],
gender : String,
store_id : String,
store_name: String,
age: Number
});
var inGiftInterSchema = new Schema({
interest: String,
dynamicScore: Number
});
this is where I'm trying to get for each gift in my DB all its interests:
function giftSearch(maxPrice ,minPrice,age, interests ,res) {
Gift.find({}).populate('interests').exec(function(err,gifter) {
res.render('resultPage');
});
}
I get an array name "gifter" that only in the second place (gifter[1]._doc.interests) i have the models (inGiftInterSchema) that I wanted, in all of the other 6 gifts that I have in gifter (and in my DB) the intetersts array size is 0...
THANKS! :)
Related
I have a User collection and a Task Collection. Many Users can have many Tasks, and Many Tasks can have many Users. The proper way I believe in doing this is the following models:
var UserSchema = new Schema({
id: ObjectId,
username: { type: String, required: true },
assignments: [ {type : mongoose.Schema.ObjectId, ref : 'Assignment'} ]
});
var TaskSchema = new Schema({
id: ObjectId,
title: { type: String, default: '' },
information: { type: String, default: '' },
assignments: [ {type : mongoose.Schema.ObjectId, ref : 'Assignment'} ]
});
var AssignmentSchema = new Schema({
id: ObjectId,
isCompleted: { type: Boolean, default: false },
completionDate: { type: Date, default: null },
tasks: [ {type : mongoose.Schema.ObjectId, ref : 'Task'} ],
users: [ {type : mongoose.Schema.ObjectId, ref : 'User'} ]
});
If the above models are correct, how do you insert a Task with multiple user assignments? I understand that you would create the Task document first to get its ObjectId, but after that would you just insert all of the assignments into the Assignment collection (with their proper Task and User objectId's) and thats it? Or would I have to insert all of the assignments then edit each individual User and Task to insert the AssignmentId into their assignments property.
I am sure there is a stack over flow question like this already, but I have not been able to find one. Any help is appreciated!
I believe you have the answer in your question.
Create a new task, capture the task id. Then find or create a new user, edit or add the task’s id, capture the user id. Add the user id to the task. Repeat for additional users.
I believe this is what you say in the final part of your question?
I don’t see why this cannot work.
I have a mongoose schema which has an array I want to reference a document
that is nested inside that array from another schema. How can I go about it.
This is my schema
var userSchema = mongoose.Schema({
username: String,
first_name : String,
last_name : String,
exam_test_id :{
type : Schema.Types.ObjectId,
ref : 'departmentSchema.members'
}
});
var departmentSchema = mongoose.Schema({
name : String,
description : String,
members :[{
commencement_date : Date,
post : String,
function : String
}]
})
I am confused whether that ref is appropriate, if it is not right is there another
way I can go about it while still have a nested sub document
I have two Mongo schemas defined as follows:
var userSchema = new mongoose.Schema({
email: String,
password: String, //hash created from password
firstName: String,
lastName: String,
comment:{userComment:String,adminComment:String},
postalAddress: String,
city: String,
state: String,
country: String,
institution: String,
privilege: {type: String, enum:['normal','chair','admin']},
status: {type:String, enum: ['granted','removed','pending']},
myConference:[{type:Schema.Types.ObjectId,ref:'Conference'}],
mySubmission:[{type:Schema.Types.ObjectId,ref:'Submission'}]
});
var conferenceSchema = new mongoose.Schema({
conferenceTitle: {type:String},
conferenceDescription: String,
conferenceStartDate:{type:Date, default: Date.now},
submissionEndDate:{type:Date},
reviewEndDate:{type:Date},
**conferenceMembers:[{type:Schema.Types.ObjectId,ref:'User'}]**,
conferenceSubmissions:[{type:Schema.Types.ObjectId,ref:'Submission'}],
createdBy:{type:Schema.Types.ObjectId,ref:'User'},
//chairMembers:[{type:Schema.Types.ObjectId,ref:'User'}],
department:String
});
Requirement: I want to fetch all the Conference objects which match a certain _id i.e. unique for each 'User' schema object.
conferenceMembers is an array of 'User' objects
What I did:
It's a POST:
var userId=req.body.userId
**Conference.find({userId: {$in: [Conference.conferenceMembers]}},function(err,conf){**
if(err){
return res.send(500, err);
}
return res.send(200,conf);
But, the filter doesn't seem to work here, I tried with $elemMatch as well but no luck.
To fetch all the documents which has specific userId in conferenceMembers, you can do this:
Conference.find({conferenceMembers : userId}).exec(function(err,conf){...});
if you want to populate the users too you can use mongoose populate.
Conference.find({conferenceMembers : userId}).populate('conferenceMembers').exec(function(err,conf){...});
My API currently has a route for Getting an event from my MongoDB database based on event_id. This works fine. However, I have a 'photos' array within this event object that is growing (currently over 3,000 objects within this array).
I want to pass a limit parameter to limit the number of results pulled from this array, but cannot figure out how. Below is my current node route and mongoDB schema:
route:
// get event by _id
app.get('/api/events/:event_id', function(req, res) {
// use mongoose to get event
Event.findOne({object_id: req.params.event_id}, function(err, event) {
// if there is an error retrieving, send the error. nothing after res.send(err) will execute
if (err)
res.send(err)
if (req.params.limit >= 0) {
// res.jsonp(event) with photos array limited to req.params.limit
}
res.jsonp(event); // return event in JSON format
});
});
schema:
var eventSchema = new Schema({
event: String,
city: String,
state: String,
date: String,
start: String,
end: String,
dateState: String,
radius: String,
team_1: String,
team_2: String,
object_id: String,
longitude: String,
latitude: String,
cover: {
img: String,
username: String
},
photos: []
})
Don't have a constantly growing array field. It's not good for performance because MongoDB (well, if <= 2.6/using mmap) will be moving the document around when it grows outside of the space allocated for it by the storage engine, causing performance problems. You should change your schema to avoid an array like this, but I can't really say more about how you should do it because I don't know much about your use case.
There is a way to limit the number of array elements returned in a find query though, using $slice projection.
> db.test.drop()
> db.test.insert({ "_id" : 0, "x" : [0, 1, 2, 3, 4] })
> db.test.find({ "_id" : 0 }, { "x" : { "$slice" : 2 } })
{ "_id" : 0, "x" : [0, 1] }
Event.findOne({object_id: req.params.event_id})
.limit(10)
.exec(function(e,doc){
...
});
Edit
Or if you have ref on the photo... you can populate the doc array of referenced id with limit option. Hope it helps :) All abount population
.find(...)
.populate({
path: 'photos',
options: { limit: 5 }
})
.exec(...)
Schema
var eventSchema = new Schema({
event: String,
city: String,
state: String,
...
photos: [{ type:String, ref:'pictureSchema' }]
}
var pictureSchema = new Schema({
name : {type:String},
url : {type:String},
...
}
In photos array than you just put id of the pictures doc, when you populate the photos array it will put pictureSceham doc insted of _id.
How can I add a schema to another schema? This doesn't seem to be valid:
var UserSchema = new Schema({
name : String,
app_key : String,
app_secret : String
})
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : UserSchema
})
I checked the website and it shows how to declare it for an array but not for single.
Thanks
There are a few ways to do this. The simplest is just this:
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : Schema.ObjectId
});
Then you just have to make sure your app is writing that id and using it in queries to fetch "related" data as necessary.
This is fine when searching tasks by user id, but more cumbersome when querying the user by task id:
// Get tasks with user id
Task.find({user: user_id}, function(err, tasks) {...});
// Get user from task id
Task.findById(id, function(err, task) {
User.findById(task.user, function(err, user) {
// do stuff with user
}
}
Another way is to take advantage of Mongoose's populate feature to simplify your queries. To get this, you could do the following:
var UserSchema = new Schema({
name : String,
app_key : String,
app_secret : String,
tasks : [{type: Schema.ObjectId, ref: 'Task'}] // assuming you name your model Task
});
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : {type: Schema.ObjectId, ref: 'User'} // assuming you name your model User
});
With this, your query for all users, including arrays of their tasks might be:
User.find({}).populate('tasks').run(function(err, users) {
// do something
});
Of course, this means maintaining the ids in both places. If that bothers you, it may be best to stick to the first method and just get used to writing more complex (but still simple enough) queries.
As of version 4.2.0, mongoose supports single subdocuments.
From the docs:
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
// Array of subdocuments
children: [childSchema],
// Single nested subdocuments. Caveat: single nested subdocs only work
// in mongoose >= 4.2.0
child: childSchema
});
What about this simple solution?
var TaskSchema = new Schema({
name : String,
lastPerformed : Date,
folder : String,
user : {
name : String,
app_key : String,
app_secret : String
}
})