Browse files

Fixed bugs and improved |findOne|

  • Loading branch information...
1 parent 5f0fe47 commit 8a3f0afe03dcc898d2072f0bb385282483a423c3 @sergi committed Apr 24, 2010
Showing with 15 additions and 13 deletions.
  1. +15 −13 lib/collection.js
View
28 lib/collection.js
@@ -99,24 +99,26 @@ Collection.prototype = {
return typeof arg == "object" ?
Support.createBDObject(arg) : arg;
});
-
return new Cursor(this.collection.find.apply(this.collection, args));
},
- findOne: function(obj) {
- var all;
- if (!obj && (parseInt((all = this.find()).count()) === 1))
- return all.next();
-
- var dbo = obj;
- if (obj && typeof(obj) != "object")
- dbo = { "_id": obj };
+ findOne: function(obj, fields) {
+ var result;
+ if (obj) {
+ if (typeof obj != "object")
+ obj = { "_id": obj };
- var sm = this.collection.findOne(Support.createBDObject(dbo), Support.createBDObject(arguments[1]));
- if (sm) {
+ result = this.collection.findOne(
+ Support.createBDObject(obj, true),
+ Support.createBDObject(arguments[1])
+ );
+ } else {
+ result = this.collection.findOne();
+ }
+ if (result) {
var jsObj = {__proto__: null},
- smKeySet = sm.keySet().toArray();
+ smKeySet = result.keySet().toArray();
for each(var i in smKeySet)
- jsObj[i] = sm.get(i);
+ jsObj[i] = result.get(i);
return jsObj;
} else
return null;

0 comments on commit 8a3f0af

Please sign in to comment.