var mongoose = require('mongoose');
var FriendSchema = new mongoose.Schema({
requester: {
type: String,
required: true,
},
recipient: {
type: String,
required: true,
},
});
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
var Friend = mongoose.model('Friend', FriendSchema);
module.exports = Friend;
I am trying to query it by using
Friend.find({"requester": { $in: [some id value]}}, function(err, fee){
console.log(fee.recipient);
});
and having it return the recipient id value..
Any suggestions would really be helpful, thank you.
you can use projection of mongo.db
the structure is like below ,
find(condition , requirefield , callback);
below is the example in which the fee will contain only the recipient
Friend.find({"requester": { $in: [some id value]}}, { _id : 0 , requester : 0 } ,function(err, fee){
console.log(fee.recipient);
});
you can refer https://docs.mongodb.com/manual/reference/method/db.collection.find/#projections for more reference.
Related
I want to join two collection in MongoDB. I have two collections 1. student 2. course.
student collection:
course collection
I tried some code but that is not working.
This is my code
student.js
router.get('/student/:id', function(req, res){
Student.find({_id:req.params.id}, function(err, user){
res.send(user);
})
})
This is Schema:
student.js
const mongoose = require('mongoose');
let StudentSchema = mongoose.Schema({
name:{
type: String
},
email:{
type: String
},
phone:{
type: String
},
password:{
type: String
},
course :[{
type: mongoose.Schema.Types.ObjectId,
ref: 'Course'
}]
}, { collection: 'student' });
const Student = module.exports = mongoose.model('Student', StudentSchema);
course.js
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let CourseSchema = mongoose.Schema({
student_id:{
type: String
},
course_name:{
type: String
},
course_cost:{
type: String
}
}, { collection: 'course' });
const Course = module.exports = mongoose.model('Course', CourseSchema);
Result:
you need to query like this :
findOne : to find single document (return object {} )
find : to find multiple documents (return array [])
Student.findOne({_id:req.params.id}, function(err, student){
if(!err && student){
Courses.find({student_id:req.params.id},function(error,courses){
if(!error && courses){
student.courses=courses;
}else{
student.courses=[];
}
res.send(student);
});
}
});
currently you are getting course :[] , because there is no field found in students collection , as you can see in photo-1
you need to set course :["coures_id1","coures_id2"] while inserting a document in student collection.
then you can use mongoose populate to populate course into students
Student.findOne({_id:req.params.id}).populate('course').exec(function(err,student){
res.send(student);
});
so after than no need to store , student_id field in courses collection , as you are getting from students collection.
Given the following schema:
var UserSchema = new Schema({
, email : { type: String }
, passwordHash : { type: String }
, roles : { type: [String] }
});
I'd like email to be the key.
How can I define this?
I could do:
var UserSchema = new Schema({
, _id: { type: String }
, passwordHash : { type: String }
, roles : { type: [String] }
});
so MongoDB would recognize it as the id-field, and adapt my code to refer to _id instead of email but that doesn't feel clean to me.
Anyone?
Since you're using Mongoose, one option is to use the email string as the _id field and then add a virtual field named email that returns the _id to clean up the code that uses the email.
var userSchema = new Schema({
_id: {type: String},
passwordHash: {type: String},
roles: {type: [String]}
});
userSchema.virtual('email').get(function() {
return this._id;
});
var User = mongoose.model('User', userSchema);
User.findOne(function(err, doc) {
console.log(doc.email);
});
Note that a virtual field is not included by default when converting a Mongoose doc to a plain JS object or JSON string. To include it you have to set the virtuals: true option in the toObject() or toJSON() call:
var obj = doc.toObject({ virtuals: true });
var json = doc.toJSON({ virtuals: true });
I have a strange one...
I've developed an api with Node/Express/Mongoose using Mongodb 3.4.9, now it's 3.4.17.
I have no ideal why, but for some reason a block of code I have been using for ages is throwing an error:
{name: "MongoError", message: "Unknown modifier: $pushAll", driver: true, index: 0, code: 9,…}
code: 9
driver: true
errmsg: "Unknown modifier: $pushAll"
index: 0
message: "Unknown modifier: $pushAll"
name: "MongoError"
Here is the code:
router.route('/addemail/:id')
// ADD EMAILS
.put(function(req, res){
Profile.findOne({'owner_id':req.params.id}, function(err, profile){
if(err)
res.send(err);
profile.emails.push({
email_type: req.body.email_type,
email_address: req.body.email_address
})
profile.save(function(err){
if(err)
res.send(err);
res.json(profile);
});
});
});
As you can see, I'm not using $pushAll in this block of code, or actually anywhere in my code.
What else could be causing this???
Thanks for any guru advise.
Update: Here is my model for the profile and I'm including the emails model next:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// SUBDOCUMENTS
var AddressesSchema = require('./profile/addresses');
var BusinessesSchema = require('./profile/businesses');
var EmailsSchema = require('./profile/emails');
var PhonesSchema = require('./profile/phones');
var SocialSchema = require('./profile/social');
// PROFILE (PARENT) MODEL
var ProfileSchema = new Schema({
//PROFILE INFO
owner_id: {
type: String,
require: true,
unique: true
},
notice: {
type: Number, // 1=profile, 2=profile and cards
},
first_name:{
type: String
},
last_name:{
type: String
},
initial:{
type: String
},
birthday:{
type: Date
},
highschool:{
type: String
},
college:{
type: String
},
facebook:{
type: String
},
linkedin:{
type: String
},
linkedin_bus:{
type: String
},
twitter: {
type: String
},
google: {
type: String
},
pinterest: {
type: String
},
user_image: {
type: String
},
contacts:[{
type:Schema.Types.ObjectId,
ref:'Contact'
}],
//SUBDOCUMENTS
emails:[EmailsSchema],
phones:[PhonesSchema],
addresses:[AddressesSchema],
businesses:[BusinessesSchema],
social:[SocialSchema]
});
module.exports = mongoose.model('Profile', ProfileSchema);
Here is what the emails model looks like:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
// CONTACT (PARENT) MODEL
var EmailSchema = new Schema({
//CONTACT INFO
email: {
type: String,
require: true
},
date_registered: {
type: Date,
default: Date.now
}
});
module.exports = mongoose.model('Email', EmailSchema);
Mongoose probably creates a $pushAll under the hood which, however, has been removed in newer version of MongoDB as you can see here. So this is why you get the error.
I suggest you upgrade to the latest version of Mongoose which will fix this.
Also see these discussions on the Mongoose repo: https://github.com/Automattic/mongoose/issues/4455
https://github.com/Automattic/mongoose/issues/5574
Pardon me for asking but why don't you just:
// ADD EMAILS
.put(function(req, res) {
Profile.update({'owner_id': req.params.id},
{
$addToSet: {
email_type: req.body.email_type,
email_address: req.body.email_address
}
});
});
It seems you just want to add an object to an array in a mongo document based on owner_id. $addToSet does that.
You should get advantage of some mongodb nice features i.e. you could do these:
Profile.findOneAndUpdate({'owner_id':req.params.id},{addToSet:{emails:[ email_type: req.body.email_type, email_address: req.body.email_address]}}, function(err, profile){
if(err){
res.send(err);
} else {
res.json(profile);
}
}
I am using mongoose with node.js application. I don't want _id field in record.
I am using this code to save my record without _id field. But it is giving error
document must have an _id before saving
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var PlayerSchema = new Schema({
player_id : { type: Number },
player_name : { type: String },
player_age : { type: Number },
player_country : { type: String }
}
, { _id: false }
);
var Player = mongoose.model('Player', PlayerSchema );
var athlete = new Player();
athlete.player_id = 1;
athlete.player_name = "Vicks";
athlete.player_age = 20;
athlete.player_country = "UK";
athlete.save(function(err) {
if (err){
console.log("Error saving in PlayerSchema"+ err);
}
});
I am using mongoose version 3.8.14
Unfortunately, You can not skip having a primary key for the document but you can override the primary key content, you can define your own primary key for each document.
Try the following schema for the same.
var PlayerSchema = new mongoose.Schema({
_id : { type: Number },
player_name : { type: String },
player_age : { type: Number },
player_country : { type: String },
}
);
I have replaced your player_id with _id. Now you have control over the primary key of the document and the system won't generate the key for you.
There are some plugins which can also do the autoincremet for your primary key. https://github.com/chevex-archived/mongoose-auto-increment. You might try these as well.
Also, about the error you are getting :
Any document is an object and should be wrapped inside the curly brackets you can not define two independent object in the same document. So you are getting this error.
Copying an answer given on a similar Github issue comment:
The _id option exists to prevent one being created and assigned to
every sub-document within your document.
So, it's not for this:
var Schema = mongoose.Schema,
ThingSchema = new Schema({
name: String,
categories: [{
label: {
type: String,
uppercase: true
},
value: String
}]
}, {_id: false});
// ^^^^^^^^^
// this won't work
It's for this:
var Schema = mongoose.Schema,
ThingSchema = new Schema({
name: String,
categories: [{
label: {
type: String,
uppercase: true
},
value: String,
_id: false // <--- this works
}]
});
Took me a while but, after running into the same problem, I found the
documentation here.
instade of writing this
{
player_id : { type: Number },
player_name : { type: String },
...}
write this:-
{
_id : { type: Number },
_name : { type: String },
_age : { type: Number },
_country : { type: String }
}
hence _id will be players_id
then further write code like this:-
var athlete = new Player();
athlete._id = 1;
athlete._name = "Vicks";
athlete._age = 20;
athlete._country = "UK";
athlete.save(function(err) {
if (err){
console.log("Error saving in PlayerSchema"+ err);
}
});
//MONGOOSE SCHEMA OBJECT
var userSchema = new Schema( {
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
tags:[ {name : String, color : String } ],
bookmarks:[{link : String, tags:[ {name : String, color : String } ]}]
} );
module.exports = userSchema; //Export the userSchema
var UserModel = mongoose.model('UserModel', userSchema ); //Create Model
module.exports = UserModel; //Export the Model
//I CAN DELETE AN ITEM FROM BOOKMARKS ARRAY NO PROBLEM USING
UserModel.findByIdAndUpdate(userId ,{$push : {bookmarks: {link : req.body.link, tags : req.body.tags}}}, function(err, user_data) {
PROBLEM!!
How do I delete a tag from the tags Array, within the bookmarks array given users _id, bookmarks _id and the tag _id or name?
//I have tried variations of the following without success
var update = {bookmarks:[{ _id : bookmarkId},
$pull: {tags:[_id : tagid ] }] };
UserModel.findByIdAndUpdate(userId ,update, function(err, user_data) {
AND
UserModel.findOne( {_id : userId}).select({ bookmarks:[ { $elemMatch: {_id : req.params.bookmarkId}}] }).exec(function(err, user_data)
Initially I was using different Models and subdocuments.
var bookmarkSchema = new Schema( {
link : String,
tags:[tagSchema]
});
var tagSchema = new Schema( {
name : String,
color : String
});
var userSchema = new Schema( {
username: { type: String, required: true, unique: true },
email: { type: String, required: true, unique: true },
tags:[ {name : String, color : String } ],
bookmarks: [bookmarkSchema]
} );
However, I was unable to delete items from the subdocuments using the $pull command like I used above. So I reverted to just using one schema/model.
This is a very important task to be able to complete and I would be greatful for help.
Thanks Muhammad but I could not get either of the 2 methods to work:
1) Nothing happens to DB and the values of the callbacks are:
*numberAffected: 0
*raw: {"updatedExisting":false,"n":0,"connectionId":322,"err":null,"ok":1}
UserModel.update({bookmarks:req.params.bookmarkId},
{ $pull: {"bookmarks.tags" :{_id:req.body._id, name :req.body.name ,color:req.body.color}}}, function(err, numberAffected, raw) {
2) I had to use the lean() function to convert Mongoose Document data format to normal JSON. Otherwise bookmarks.push({link:user.bookmarks[i].link,_id:user.bookmarks[i]._id,tags:tags})
would not combine properly with:
bookmarks.push(user.bookmarks[i]);
on bookmarks[]
However using the lean() functions means I would not be able to save the data to the DB with .save
UserModel.findById(userId).lean(true).exec(function(err, user) {
delete from sub-Document of JSON doc,
UserModel.update({bookmarks:bookmarkId},{$pull: {"bookmarks.tags" :{_id:tagsId,name :name ,color:color}}}, function(err, user_data) {
or
UserModel.findOnd(userId,function(er,user){
var bookmarks= [];
var tags = [];
for (var i = 0;i<user.bookmarks.length;i++){
if (user.bookmarks[i]._id==bookmarkId)
{
for (var j = 0;j<user.bookmarks[i].tags.length;j++){
if (user.bookmarks[i].tags[j]._id==tagid )
{
}else {
tags.push(user.bookmarks[i].tags[j])
}
}
bookmarks.push({link:user.bookmarks[i].link,_id:user.bookmarks[i]._id,tags:tags})
}
else {
bookmarks.push(user.bookmarks[i]);
}
}
})