Sails with Mongo中的本机更新不适用于ObjectId [英] native update in Sails with Mongo does not work with ObjectId

查看:80
本文介绍了Sails with Mongo中的本机更新不适用于ObjectId的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道我在做错什么.

I wonder what I am doing wrong.

我使用Sailsv0.10和mongo2.6.0,并希望通过本机更新集合中的数组字段(使用$ push).

I use Sailsv0.10 and mongo2.6.0 and want to update an array field (using $push) in a collection via native.

我的模特:

module.exports = {

schema: true,
attributes: {

  username: {
    type: 'string',
    required: true
  },
  pubs: {
    type: 'array',
    defaultsTo: []
  },
  ...

我的功能:

    User.native(function (err, collection) {
      collection.update({username:aUsernameVariable},{$push:{pubs:aPubsVariable}}, function (err) {
    });

到目前为止有效.但是,为什么这不能用作带有id字段的查询呢?

It works so far. But why does that not work as a query with the id field?

    User.native(function (err, collection) {
      collection.update({id:anIdVariable},{$push:{pubs:aPubsVariable}}, function (err) {
    });

我一定要为查询使用正确的ID进行测试.

I definately use the right id for the query to test it.

我做错了什么?还是Sails-Mongo适配器的ObjectId类型转换问题

What am I doing wrong? Or is that a ObjectId type conversion Problem of the Sails-Mongo Adapter

推荐答案

如果您想使用native(),则始终可以直接在mongo-DB中尝试相同的查询.由于_id是对象ID,您应该

If you want to use native() you can always try the same query directly in your mongo-DB. Because _id is a Object-id you should

var ObjectId = require('mongodb').ObjectID;

 User.native(function (err, collection) {
  collection.update({_id: new ObjectId(anIdVariable)},{$push:{pubs:aPubsVariable}}, function (err) {
});

您可以使用npm install mongodb --save

这篇关于Sails with Mongo中的本机更新不适用于ObjectId的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆