Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cryptic mongodb error LEFT_SUBFIELD only supports Object: stats not: 6

I'm having trouble figuring out what this error means

LEFT_SUBFIELD only supports Object: stats not: 6

It seems to be happening when I am inserting into my profiles collection. I am using mongoose.js. We are inserting counts of posts in each category in the stats property, e.g.

stats: {category:count, category2: count2}.

Here is my schema

var ProfileSchema = new Schema({
  uname: {
    type: String,
    required: true,
    index: true,
    unique: true
  },
  fname: String,
  lname: String,
  stats: {
    type:{},
    "default":{},
    required:true
  },
  created: {
    type:Date,
    required:true,
    "default":Date.now
  }
});

I think it might be happening when I am updating the stats object $inc counts so that stats can come out to something like this update

db.status.update({_id:xyz}, {$inc: { stats.foo : 1, stats.bar:1}})

Here's my mongoose code

      var tags = ["comedy", "action", "drama"];

      //also adding the postId to the posts collection of profile
      var updateCommand = {$push: {posts: post._id}};

      var stats = {};
      for (var i = tags.length - 1; i >= 0; i--){
        stats["stats." + tags[i].toString()] = 1;
      };
      updateCommand.$inc = stats;

      Profile.update(
        {uname: uname}, 
        updateCommand,
        {safe:true, upsert:true},
        callback
      );
like image 884
MonkeyBonkey Avatar asked Apr 20 '12 04:04

MonkeyBonkey


2 Answers

It also happens if you're trying to update a subdocument of a non-object.

> db.test.insert({_id: 10240292, object: 'some string'})
> db.test.update({_id: 10240292}, {$set: {'object.subkey': 'some string'}})
LEFT_SUBFIELD only supports Object: object not: 2

Maybe it's not your case, but it can help somebody who googles for this error.

like image 143
alex Avatar answered Nov 08 '22 18:11

alex


You may be running into this:

https://jira.mongodb.org/browse/SERVER-2651

or

https://jira.mongodb.org/browse/SERVER-5227

Both of which are fixed in the 2.1 dev branch already but not (yet) backported to 2.0

There is a decent discussion here about a similar issue:

https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/VhjhcyEdbNQ

Basically it boils down to the fact that you are likely passing an empty key as part of the update which needs to be avoided.

like image 39
Adam Comerford Avatar answered Nov 08 '22 18:11

Adam Comerford