0

I'm trying to fix some data globally... Why am i getting this error...TypeError: Object function model(doc, fields, skipId) { if (!(this instanceof model)) r

I am getting all the data and it should update, but its not. and the error is obtuse.

UpdateAllThings: function(req, res) {
    things.find().exec( function(err, items) {
        items.forEach(function(th) {
            th.Features.forEach(function(f) {
                thingsfeatures.findOne({'fName': f.fName},function(err, item) {
                    if (item != null ) {
                        try{
                            things.Update({Features: {"$elemMatch":{_id:f._id}}}, {$addToSet: {'Features.$.tType': item.tType, 'Features.$.fGroup': item.fGroup }}, function (err, inventory) {
                                if (err) return handleError(err);
                            });
                        }catch(err){
                            if (err) return handleError(err);
                        }

                    }
                });
            });
        });

    });
     return res.send('thanks please come again');
}

*_________________________*

sure: This is more or less it....

{
  "Features" : [{
      "_id" : ObjectId("5013c9742df16337609b2706"),
      "tID" : "5013c9742df16337609b26f4",
      "tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
      "fName" : "Retail Price:",
      "fValue" : "899.99",
      "_keywords " : ["899.99"]
    }, {
      "_id" : ObjectId("5013c9742df16337609b2707"),
      "_keywords " : ["1", "x", "Proprietary", "LP-E8", "Lithium-ion", "rechargeble"],
      "fGroup" : ["Power"],
      "fName" : "Batteries Included:",
      "fValue" : "1 x Proprietary LP-E8 Lithium-ion rechargeble",
      "tID" : "5013c9742df16337609b26f4",
      "tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
      "tType" : ["Cammera"]
    }],
  "_id" : ObjectId("5013c9742df16337609b26f4"),
  "tName" : "Canon EOS T2i (Rebel T2i, Canon 550D) ",
  "tType" : "Camera"
}

This Worked, but it added in an array with data and not just the new fields and data as i wanted.

db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $addToSet : { "Features.$.fType" : "Cammera", "Features.$.fGroup"  : "Power" }})
  • One problem I see: `things.Update` should be `things.update` – JohnnyHK Aug 29 '12 at 17:47
  • 1
    Sun of a bitch. Thanks johnny... Arg I hate case sensitive languages. – athingsiathing Aug 29 '12 at 18:17
  • But the thing is it is still not updating the records with the new fields and data... though I test the command and it is working.. well not throwing an error. db.things.update({ "Features" : { "$elemMatch" : { "Features._id" : ObjectId("5013c9742df16337609b2707") } } }, { "$addToSet" : { "Features.$.tType" : "Cammera", "Features.$.fGroup" : "Power" } }) – athingsiathing Aug 29 '12 at 18:18
  • > db.runCommand("getlasterror") { "updatedExisting" : false, "n" : 0, "connectionId" : 2, "err" : null, "ok" : 1 } – athingsiathing Aug 29 '12 at 20:32
  • Can you provide details on your schema? It's very hard to figure out what you're trying to do. – JohnnyHK Aug 29 '12 at 22:10
  • sure: db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $addToSet : { "Features.$.fType" : "Cammera", "Features.$.fGroup" : "Power" }}) – athingsiathing Aug 29 '12 at 22:23
  • No, I mean update your original post to contain an example document; something that makes it clear how your documents are structured. – JohnnyHK Aug 29 '12 at 22:27
  • Your query looks fine to me for that document structure. – JohnnyHK Aug 30 '12 at 17:58

1 Answers1

0

Issue one Capitalization. Thanks Johnny

Issue Two: Addtoset creates array's and not add's to the set that is there it creates new set's. So just use set. (confusing documentation personaly)

Third: To update nested set's simply do it like this: This is much better then using elementmatch and other forms.

db.things.update({"_id":ObjectId("5013c9742df16337609b26f4"), "Features._id" : ObjectId("5013c9742df16337609b2706") }, { $set : { "Features.$.fType" : "Cammera", "Features.$.fGroup"  : "Power" }})
Anuj Balan
  • 7,629
  • 23
  • 58
  • 92