Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model.insertOne() - returns array #25

Closed
Miscellaneous opened this issue Aug 28, 2015 · 6 comments
Closed

Model.insertOne() - returns array #25

Miscellaneous opened this issue Aug 28, 2015 · 6 comments
Labels

Comments

@Miscellaneous
Copy link

Seems to me, like Model.insertOne() will return an array with 1 object, instead of just the object.

@jedireza
Copy link
Owner

Thanks for opening an issue. Do you mean it should return an array or that it does and the docs are wrong?

@Miscellaneous
Copy link
Author

I would imagine, .insertOne() would return an object (without an nesting array).

I get:
result = [ {foo:1} ]

expected:
result = {foo:1}

I base this upon .findOne() or .findById() does indeed only return an object (without the nesting array).

@jedireza
Copy link
Owner

Ah yes, I remember this. We're simply proxying the call to the driver's insertOne method:

collection.insertOne.apply(collection, args);

According to the docs:
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#insertOne

insertOne has a writeOpCallback callback:
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#~writeOpCallback

Which defines the result as a WriteOpResult
http://mongodb.github.io/node-mongodb-native/2.0/api/Collection.html#~WriteOpResult

If we chose to change this, it would be a major version bump as the API would change. Is it really worth it?

@Miscellaneous
Copy link
Author

I'm new to Mongo, so not really in any position to advise on what is worth it.

I just believed to have discovered an Inconsistency error, but being an artifact from the driver, it might be outside your domain.

Appreciate your framework and your kind support, I hope its not to much trouble too ask.

@jedireza
Copy link
Owner

I just believed to have discovered an Inconsistency error, but being an artifact from the driver, it might be outside your domain.

We could change it in this library, but I prefer to do as little as possible in that regard.

Appreciate your framework and your kind support, I hope its not to much trouble too ask.

That's always nice to hear. Thanks :) Always feel free to ask questions.

@Miscellaneous
Copy link
Author

Model.insertOne() - Workaround to output an object, instead of array with an object

                        Model.insertOne( schema, function( err, model ){
                            if( err ) return reply( err );
                            return reply( model.shift() );
                        });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants