4

I wonder what I am doing wrong.

I use Sailsv0.10 and mongo2.6.0 and want to update an array field (using $push) in a 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) {
    });

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) {
    });

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

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

user3707805
  • 105
  • 2
  • 7

3 Answers3

11

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) {
});

You can add the mongo-native-driver to you app with npm install mongodb --save

mdunisch
  • 3,627
  • 5
  • 25
  • 41
  • Thanks for answering. If you mean "query directly in your mongo-DB" in the way of using the mongo shell, then would say I want to do that command from a Sails controller. In Sails ObjectId type is unknown. Id is getting internally converted in normal Sails functions, but not in native update obviously. In native find it works too, but not in native update. – user3707805 Sep 06 '14 at 19:27
  • Oh sorry, I forgot to add the mongodatabase-driver. I updated my solution! – mdunisch Sep 15 '14 at 17:09
  • Thank you very much! With "var ObjectId..." instead of "var ObjectID..." it works perfectly. It saves me some indexes, I guess. Since I am a noob, are there any disadvantages doing it this way? – user3707805 Sep 15 '14 at 22:10
10

if you use sailsjs then:

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

:)

rhernandez@itemsoft.mx

Richard
  • 95
  • 2
0

The problem is because, in mongo relation field, data stores as string - but should be as ObjectID, e.g. ObjectID("56309f327dc5a4133c54bd5e"). I've tried to generate ObjectID in beforeCreate() function

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

But it throw an error "Argument passed in must be a single String of 12 bytes or a string of 24 hex characters new ObjectId". I had sails-mongo 0.11.2. Then I'v tried to update - and it helped to me. So check your sails-mongo version. Do not forget to remove manual generating ObjectID from beforeCreate() it will deals by itself

beforeCreate: function(values, cb) {

    cb();
}
1nstinct
  • 1,745
  • 1
  • 26
  • 30