marcello3d / node-mongolian

[project inactive] Mongolian DeadBeef is an awesome Mongo DB driver for node.js
https://groups.google.com/group/node-mongolian
zlib License
349 stars 50 forks source link

Save callback gets no data #31

Closed bingomanatee closed 13 years ago

bingomanatee commented 13 years ago

When I call the script below, the save callback is not fed anything.

var Mongolian = require('mongolian');

module.exports.run = function() {

    var server = new Mongolian();
    var db = server.db('ac');
    var foo = db.collection('foo');

   // console.log('f == ', foo);
    var d = {
        a: 1,
        b: 6
    };

    foo.insert(d);

    foo.findOne({
        b: 6
    }, function(err, f) {
        console.log('found ', f);

        f.c = 3;

        foo.save(f,  function(err, f2) {
            console.log('saved: ', err, f2);

            foo.findOne({
                b: 6
            }, function(err, f) {
                console.log('found again ', f);
            })
        });
    })

}

/* result loading test /Users/dave/Documents/Arena_Colles/test/ins_upd.js mongo://localhost:27017: Connected mongo://localhost:27017: Initialized as primary mongo://localhost:27017: Connected to primary Finished scanning... primary? mongo://localhost:27017 found { a: 1, b: 6, _id: 4e15cfc9fa62237e01000001 } saved: null undefined found again { a: 1, b: 6, _id: 4e15cfc9fa62237e01000001, c: 3 } /

bingomanatee commented 13 years ago

Further reference - the difference seems to be the presence of the manually chosen _id:

var Mongolian = require('mongolian');

module.exports.run = function() {

    var server = new Mongolian();
    var db = server.db('ac');
    var foo = db.collection('foo');

    var d2 = {z: 1, y: 2};

    foo.save(d2, function(e, r){
        console.log('saved ', d2, ' as ', r);
    })

    foo.findOne({
        z: 1
    }, function(err, result) {
        console.log('found ', result);
    });

    var data = {
        _id: 1,
        name: "admin"
    };

    foo.save(data, function(e, r){
        console.log('saved ', data, ' as ', r);
    });

    foo.findOne({name: 'admin'}, function(err, r){

        console.log('found ', r);
    })

}

results in

^CAcropolis:Arena_Colles dave$ node test.js set_id.js loading test /Users/dave/Documents/Arena_Colles/test/set_id.js mongo://localhost:27017: Connected mongo://localhost:27017: Initialized as primary mongo://localhost:27017: Connected to primary Finished scanning... primary? mongo://localhost:27017 saved { z: 1, y: 2, _id: 4e15d8957ad4f53b02000001 } as { z: 1, y: 2, _id: 4e15d8957ad4f53b02000001 } found { z: 1, y: 2, _id: 4e15d8957ad4f53b02000001 } saved { _id: 1, name: 'admin' } as undefined found { _id: 1, name: 'admin' }

marcello3d commented 13 years ago

Technically save is a shorthand for upsert (form of update) or insert depending on whether the object has an _id. Updates don't actually return the object when they succeed.

Should I special case the save function to simply return the same object when it does an upsert?

andzdroid commented 13 years ago

I think the save function should have consistent return values, regardless of whether the object has set _id, so yes, I vote for changing it.

PS. you haven't created a git tag for the latest version yet. btw, keep up the work, I love this module!

bingomanatee commented 13 years ago

Yeah - I've been to tons of "Brian pimps Mongoose" meetups and with all due respect to the power features I much more prefer the DIY approach to modeling and keeping all the moving parts accessible.

I think the design would be more parallel with a returned object in save.