Native update in Sails with Mongo does not work with ObjectId

I wonder what I'm doing wrong.

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

My model:

module.exports = {

schema: true,
attributes: {

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

My function:

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

He is still working. But why does this not work as a request with an id field?

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

I definitely use the correct identifier for the request to validate it.

What am I doing wrong? Or is it an ObjectId type conversion problem for the Sails-Mongo adapter

+4
source share
3 answers

If you want to use native (), you can always try the same query directly in your mongo-DB. Since _id is an object identifier, you must

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

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

mongo-native-driver npm install mongodb --save

+10

sailsjs, :

ObjectID = require('sails-mongo/node_modules/mongodb').ObjectID;
var o_id = new ObjectID(req.param('id'));
console.log(o_id );

:)

rhernandez@itemsoft.mx

+9

, mongo , ObjectID, . ObjectID("56309f327dc5a4133c54bd5e"). ObjectID beforeCreate()

beforeCreate: function(values, cb) {
    var ObjectID = require('mongodb').ObjectID;
    values.owner = ObjectID(values.owner);
    cb();
}

", String 12 24 new ObjectId". - 0.11.2. - . -. ObjectID beforeCreate(),

beforeCreate: function(values, cb) {

    cb();
}
0

All Articles