I'm quite new with mongoose, I have a list of companies and each company has a subarray of users. I just want to retrieve with mongoose all users of a specific company:
a single company is like
{
"_id": "57ffa47f5b70f90831212348",
"name": "mycompany",
"address": "...",
"phone": "...",
"users": [
{
"_id": "57ffa47f5b70f90831212347",
"username": "alpha",
"name": "myname",
"surname": "mysurname",
"password": "..."
}
]
}
I tried with
Company.findOne({
'name': req.user.name
})
.aggregate({$unwind: '$users'})
.exec(
function(err, users) {
if (err) res.status(500).send(err);
res.json(users);
});
but I had no luck... I'm not sure how to use aggregate correctly.
Just populate company.users
Company.findOne({
'name': req.user.name
})
.populate('users', 'username name surname')
.exec(
function(err, company) {
if (err) res.status(500).send(err);
res.json(company.users);
});
Related
"_id": {
"$oid": "577cc50d10b5a6c42b26f414"
},
"firstName": "new",
"lastName": "new",
"__v": 0,
"A": [
{
"AfirstName": "AfirstName",
"AlastName": "AlastName",
"_id": {
"$oid": "577dbef2f2c9f5901f402efe"
},
"AB": [
{
"AB1firstName": "AB1firstName",
"AB1lastName": "AB1lastName",
"_id": {
"$oid": "577dd3b1495663ec2a6ca456"
}
},
{
"AB2firstName": "AB2firstName",
"AB2lastName": "AB2lastName",
"_id": {
"$oid": "577dd3bc495663ec2a6ca45a"
}
}
]
}
]
Example.update({ _id: , A._id: , A.AB._id: } ,
{"$set": {"A.$.AB": data}},
function(err,model){ console.log(model); if (err) throw err; })
This will just update the first data of AB i want to update data of AB sub-doc by _id and also want to remove the particular nested sub-doc with _id
Example.findById(_id,function(err,doc){
var result = doc.A.id(AId).AB.id(AB.ABId);
result.AB1firstName="first Name";
result.AB1lastName= "Last Name";
doc.save();
});
This will update
Each document has an _id. DocumentArrays have a special id method for looking up a document by its _id.
Example.findOne({ _id: id, A._id: Aid, A.AB._id: ABid},
function(err,model){
if (err) throw err;
var ABDoc model.A.id(Aid).AB.id(ABid);
ABDoc.AB2firstName="new value";
ABDoc.AB2lastName="new value";
model.save();
})
So what I want to do is add on to an object in the document not delete the whole thing. I have a document with an array in it and if I call findOneAndUpdate it adds what I want but gets rid of everything already in the array.
All I want to do is keep what I already have in my array and add one more item.
Here is my code:
Friends.findOneAndUpdate({facebookId: value.id}, {$set: {friends: {id: profile.id, name: profile._json.name}} }, {new: false}, function (err, friends) {
if(err){
console.log("Something went wrong when updating data!");
}
console.log("success saving user friend update");
});
And here is what happens with my array in the database:
This:
"friends": [
{
"id": "114222474148205",
"name": "Chris Chang"
},
{
"id": "903622474147290",
"name": "Johny Unitas"
},
{
"id": "685034985392094",
"name": "Mary Jane"
}
]
To this:
"friends": {
"id": "49572957395733524",
"name": "John Doe"
}
What I want is those two to be combined.
So how would I "add" an item and not "update" the item?
As Joao said above; all I you have to do is change $set to $push.
So this:
Friends.findOneAndUpdate({facebookId: value.id}, {$push: {friends: {id: profile.id, name: profile._json.name}} }, {new: false}, function (err, friends) {
instead of this:
Friends.findOneAndUpdate({facebookId: value.id}, {$set: {friends: {id: profile.id, name: profile._json.name}} }, {new: false}, function (err, friends) {
this is my cart collection which is having a price key. In my node.js code I want to view sum of price of both the documents. I tried using aggregate but didnt work
cart collection
[
{
"_id": "57244d0a05dcf1d7151ede7f",
"art_id": "57244c9505dcf1d7151ede7c",
"artist_id": "5721a528c9d28cd51f014038",
"user_id": "5721a528c9d28cd51f014038",
"price": "90"
},
{
"_id": "57244d1f05dcf1d7151ede80",
"art_id": "57244c6105dcf1d7151ede7b",
"artist_id": "5721a528c9d28cd51f014038",
"user_id": "5721a528c9d28cd51f014038",
"price": "150"
}
]
node.js code
function test(req, res, next) {
db.users.findOne({
_id: mongoskin.helper.toObjectID(req.session.user._id)
}, function(err, user) {
if (!user) {
return res.status(400).send({
status: '404 user not found'
});
}
db.cart.find({
user_id: req.session.user._id
}).toArray(function(err, result) {
if (err) return next(err);
res.send(result)
});
});
}
to add all price can use aggregate like:
db.cart.aggregate(
[
{
$group : {
_id : null,
totalPrice: { $sum: price }
}
}
]
).exec(function(error, result) {
if (err) return next(err);
res.send(result)
});
I am not being able to remove the specific eventid from the UserSchema. I searched for this and got a method using unset but that is removing the whole user while the push function is not doing anything. Is there any other way to do this?
Current UserSchema
[
{
"_id": "56593f3657e27af8245735d7",
"username": "abc",
"name": "xyz",
"__v": 0,
"favouriteid": [
{
"eventid": "5659242639e43bfc2a0836b9",
"_id": "56593f6b57e27af8245735d8"
},
{
"eventid": "5659244039e43bfc2a0836ba",
"_id": "56593f8857e27af8245735d9"
}
]
}
]
What I need-
[
{
"_id": "56593f3657e27af8245735d7",
"username": "abc",
"name": "xyz",
"__v": 0,
"favouriteid": [
{
"eventid": "5659242639e43bfc2a0836b9",
"_id": "56593f6b57e27af8245735d8"
}
]
}
]
I will be passing eventid and userid
But when I am using this code, the user gets deleted itself rather than the required field.
api.post('/removefavourites', function (req, res) {
// _id: req.body.id;
User.findById({_id: req.body.userid},
{$unset:{favouriteid: {eventid: req.body.eventid}}},
{upsert: true},
function (err) {
if(err)
return err;
else
res.json({success: true});
});
});
You can use update method with pull operation as shown below:
api.post('/removefavourites', function (req, res) {
// _id: req.body.id;
User.update({_id: req.body.userid},
{$pull:{favouriteid: {eventid: req.body.eventid}}},
function (err) {
if(err)
return err;
else
res.json({success: true});
});
});
Hope this helps :)
I have a very simple mongoose query, that returns correctly but the field display ordering is not consistent. Notice how sometimes it lists _id and then username and sometimes it lists username and then _id. Any way to fix this?
exports.list = function(req, res){
return db.userModel.find({}, 'username', function (err, users) {
if(!err) {
return res.json(users);
} else {
console.log(err);
return res.send(400);
}
});
}
Output
[
{
"username": "admin",
"_id": "5145293a420135521c000001"
},
{
"username": "bob",
"_id": "5145293b420135521c000002"
},
{
"_id": "5145293b420135521c000003",
"username": "example"
}
]