Node.js findAndModify不';t插入要设置的完整文档

Node.js findAndModify不';t插入要设置的完整文档,node.js,mongodb,Node.js,Mongodb,我正在使用nodejs本机mongodb客户端运行以下程序: db.collection("users").findAndModify( { _id:ObjectID.createFromHexString(id), "profiles._id":pid}, {}, { '$addToSet' : {'profiles.$.categories': category}}, {new:true}, function(err, user){ if(err)

我正在使用nodejs本机mongodb客户端运行以下程序:

  db.collection("users").findAndModify( 
    { _id:ObjectID.createFromHexString(id), "profiles._id":pid}, {},
    { '$addToSet' : {'profiles.$.categories': category}}, {new:true},
    function(err, user){
       if(err){
          res.json(err);
       } 
    }
  );
我有一个类别要添加到类别集中,问题是在运行后,我在集合中看到一个新类别,但作为类别一部分的items数组被设置为null,并且未保存。 这意味着类别如下所示:

{
   name:'n1',
   items:['it1', it2']
}
{
  _id: ...      
  profiles: [
    {
      _id: ...
      categories: [
        {
           name:'c1',
           items:['it1', it2']
        },
        {
           name:'c2',
           items:['it3', it4']
        }            
      ]

    }
  ]
}
完整文档如下所示:

{
   name:'n1',
   items:['it1', it2']
}
{
  _id: ...      
  profiles: [
    {
      _id: ...
      categories: [
        {
           name:'c1',
           items:['it1', it2']
        },
        {
           name:'c2',
           items:['it3', it4']
        }            
      ]

    }
  ]
}

这与我在Mongo shell中的预期一样有效:

> var id = ObjectId(), pid = ObjectId();
> db.users.insert({
...     _id: id,
...     profiles: [
...         {
...             _id: pid,
...             categories: [
...                 {
...                     name: 'c1',
...                     items: ['it1', 'it2']
...                 },
...                 {
...                     name: 'c2',
...                     items: ['it3', 'it4']
...                 }
...             ]
...
...         }
...     ]
... });
>
> var category = {
...    name:'n1',
...    items:['it1', 'it2']
... };
>
>
> db.users.findAndModify({
...     query: { _id: id, "profiles._id": pid},
...     update: {
...         '$addToSet' : {'profiles.$.categories': category}
...     },
...     new:true
... });
{
    "_id" : ObjectId("51bfc532c0a62b206198663e"),
    "profiles" : [
        {
            "_id" : ObjectId("51bfc532c0a62b206198663f"),
            "categories" : [
                {
                    "name" : "c1",
                    "items" : [
                        "it1",
                        "it2"
                    ]
                },
                {
                    "name" : "c2",
                    "items" : [
                        "it3",
                        "it4"
                    ]
                },
                {
                    "name" : "n1",
                    "items" : [
                        "it1",
                        "it2"
                    ]
                }
            ]
        }
    ]
}

你能提供足够的信息让我们测试你的问题吗?在第一个代码示例中,
类别
的值是多少?与第二个代码示例相同吗?你能提供收藏中的一份文件吗?是的,我不想问这个问题。我可以在
查找和修改
之前和之后查看文档吗?所以这可能是nodejs驱动程序的问题?