These are chat archives for Automattic/mongoose

28th
Apr 2016
pixelul
@pixelul
Apr 28 2016 07:07
@vladotesanovic thanks looking into it right now :)
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 07:12
@pixelul great
pixelul
@pixelul
Apr 28 2016 07:14
i see it does not have query builder for update/set
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 07:17
Can you copy your document as-is here? @pixelul
pixelul
@pixelul
Apr 28 2016 07:21
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 07:27
and you want to update consumers at position 1 for example?
@pixelul
pixelul
@pixelul
Apr 28 2016 08:13
yeah but not by position
i want to update by id
managed to do a find but cannot modify it to update :)
 db.emaillists.find(
    { "_id": ObjectId("5719fe21e78b06f20616ee00"), "consumers._id": ObjectId("572072e4260cd05e07e5df30")}, 
    {consumers: {$elemMatch: {_id: ObjectId("572072e4260cd05e07e5df30")}} } 
);
this works
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 08:15
@pixelul ok let me try
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 08:20
Ok, i do this in plain mongo
Insert data: db.test.insert({data: "data", consumers: [{_id: ObjectId(), data: 1}, {_id: ObjectId(), data: 2}]})
Update second: db.test.update({"consumers._id": ObjectId("5721c7752900495d6fc0877f")}, {$set: { "consumers.$.data": 10}})
pixelul
@pixelul
Apr 28 2016 08:21
let me try it out
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 08:21
Now, it looks like this @pixelul
db.test.find().pretty()
{
    "_id" : ObjectId("5721c7752900495d6fc08780"),
    "data" : "data",
    "consumers" : [
        {
            "_id" : ObjectId("5721c7752900495d6fc0877e"),
            "data" : 1
        },
        {
            "_id" : ObjectId("5721c7752900495d6fc0877f"),
            "data" : 10
        }
    ]
}
pixelul
@pixelul
Apr 28 2016 08:23
works
damn :D
thanks alot
got another small question
for future
doing just a search by subid consumers._id
wouldnt be slow later if i have multiple entries with thousands of subconsumers ?
thats why i tried the aproach of selecting the main document by id and then the subdocument by id
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 08:26
@pixelul Sure, it is much better to have precise select
pixelul
@pixelul
Apr 28 2016 08:28
hmm i think it works also like this
db.emaillists.update({"_id": ObjectId("5719fe21e78b06f20616ee00"), "consumers._id": ObjectId("572072e4260cd05e07e5df30")}, {$set: { "consumers.$.email": 'again@db.com'}})
this should be alot faster ?
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 08:29
@pixelul yes
pixelul
@pixelul
Apr 28 2016 08:29
great
thanks so much
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 08:31
@pixelul :beers:
pdiogo
@PedroMD
Apr 28 2016 10:29
@pixelul I'm having a problem which seems to be related to yours
I can not update a sub document like you're doing
Mongoose syntax is not ok and I dont know why
User.update({_id: doc.to, unshipedItems.item: doc.item}, {$inc: {unshipedItems.$.counter: 1 }}, function(err, result){ if (err) handleError(err); console.log(result); });
My UserSchema also has a sub-document like: unshipedItems: [unshipedItemsSchema],
which is: var unshipedItemsSchema = new mongoose.Schema({ item: { type: Schema.Types.ObjectId, ref: 'User'}, counter: Number });
doing all of these on a post-hook (middleware) whenever a new item is saved under a certain condition
@pixelul did you manage to code your latest posted query using Mongoose?
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:37
@PedroMD Did you try to find element based on first argument {_id: doc.to, unshipedItems.item: doc.item} ?
pdiogo
@PedroMD
Apr 28 2016 10:38
that might be the problem, because unshipedItems.item: doc.item might not exist at that time. I must use upsert:true, right? I only need it to create the sub-document if it does not exist - not the parent one
pixelul
@pixelul
Apr 28 2016 10:39
@PedroMD add .exec(
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:40
@pixelul he uses callback
pdiogo
@PedroMD
Apr 28 2016 10:40
@pixelul its the same thing
pixelul
@pixelul
Apr 28 2016 10:41
oh sry did not saw
pdiogo
@PedroMD
Apr 28 2016 10:41
np ;)
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:42
@PedroMD yes in this case you must use uppsert option
pdiogo
@PedroMD
Apr 28 2016 10:45
@vladotesanovic its a syntax problem, as node.js is complaining about a "." char; I can not see where exactly though, as this file is bellow a module.export.attach, so node.js can trace it back to the exact line where the syntax error is
but I can not figure out what is wrong
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:45
{"_id": doc.to, "unshipedItems.item": doc.item}
{$inc: {"unshipedItems.$.counter": 1 }}
pdiogo
@PedroMD
Apr 28 2016 10:46
holy crap --'
i can't believe my eyes
the query worked
did not update, but it worked
{ ok: 0, n: 0, nModified: 0 }
multi
i might need that option as well
nops, still 0
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:51
try to remove ushippedItems.item
to test
pdiogo
@PedroMD
Apr 28 2016 10:51
still 0
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:52
@PedroMD you can try this as well
MyModel.findOneAndUpdate(query, {update}, {upsert:true}, function(err, doc){
    if (err) return res.send(500, { error: err });
    return res.send("succesfully saved");
});
pdiogo
@PedroMD
Apr 28 2016 10:52
im looking at the valid options here http://mongoosejs.com/docs/api.html#model_Model.update
Yes, i've tried that before but the syntax was wrong
will try again
the problem is that it might not find it, as the query would have two "where" statements: the parent.id and the child.id, where child.id might not exist at the time
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 10:58
Yes, that is problem. I think it will not work.
pdiogo
@PedroMD
Apr 28 2016 10:59
User.findOneAndUpdate({"_id": this.to, "unshipedItems.item": doc.item}, {$inc: {"unshipedItems.$.counter": 1 }},
    {upsert: true},
    function(error, doc){
      if(error) console.log(error);
      else console.log('doc: ', doc);
    });
{ [MongoError: exception: The positional operator did not find the match needed from the query. Unexpanded update: unshipedItems.$.counter] name: 'MongoError', message: 'exception: The positional operator did not find the match needed from the query. Unexpanded update: unshipedItems.$.counter', errmsg: 'exception: The positional operator did not find the match needed from the query. Unexpanded update: unshipedItems.$.counter', code: 16836, ok: 0 }
looks like it can not $inc a non existing variable, even though i'm using upsert
will add it manually first
Vlado Tesanovic
@vladotesanovic
Apr 28 2016 11:04
yes, it is logically impossible, how to increment something that does not exists
pdiogo
@PedroMD
Apr 28 2016 11:07
yeah, it might work once I figure out its value first
will update you soon ;)
santi macia
@santimacia
Apr 28 2016 15:59
One question about population, It's possible to exclude the elements that can't be populated? I'm trying to avoid the typical problem of lost _id
Kevin Whitman
@keverw
Apr 28 2016 23:38
What would happen if you didn't include a shardkey but the cluster is shard? Say maybe you added a shard and not all servers were updated/restarted with Mongoose having the shardkey? I was trying to learn more about the shardkey, and figured the native driver would have info about it but couldn't find anything about it on that. I figured that might give a hint since Mongoose is based on it