Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' into connectionEvents

  • Loading branch information...
commit 3bebacf593ab9d1ae7aad7760d3b5568a42bcd3a 2 parents 1b0b38b + 585e736
@aheckmann aheckmann authored
View
3  lib/schematype.js
@@ -23,6 +23,9 @@ function SchemaType (path, options, instance) {
this.selected;
for (var i in options) if (this[i] && 'function' == typeof this[i]) {
+ // { unique: true, index: true }
+ if ('index' == i && this._index) continue;
+
var opts = Array.isArray(options[i])
? options[i]
: [options[i]];
View
12 lib/types/array.js
@@ -377,6 +377,18 @@ MongooseArray.prototype.unshift = function () {
};
/**
+ * sort
+ *
+ * @api public
+ */
+
+MongooseArray.prototype.sort = function () {
+ var ret = [].sort.apply(this, arguments);
+ this._registerAtomic('$set', this);
+ return ret;
+}
+
+/**
* Adds values to the array if not already present.
* @api public
*/
View
11 test/schema.type.test.js
@@ -15,4 +15,15 @@ describe('schematype', function(){
var a = new Schema({ thought: { type: String, select: true }});
assert.equal(true, a.path('thought').selected);
})
+
+ it('properly handles specifying index in combination with unique or sparse', function(){
+ var s = new Schema({ name: { type: String, index: true, unique: true }});
+ assert.deepEqual(s.path('name')._index, { unique: true });
+ var s = new Schema({ name: { type: String, unique: true, index: true }});
+ assert.deepEqual(s.path('name')._index, { unique: true });
+ var s = new Schema({ name: { type: String, index: true, sparse: true }});
+ assert.deepEqual(s.path('name')._index, { sparse: true });
+ var s = new Schema({ name: { type: String, sparse: true, index: true }});
+ assert.deepEqual(s.path('name')._index, { sparse: true });
+ })
})
View
50 test/types.array.test.js
@@ -778,6 +778,56 @@ describe('types array', function(){
});
})
+ describe('sort()', function(){
+ it('order should be saved', function(done){
+ var db = start();
+ var M = db.model('ArraySortOrder', new Schema({ x: [Number] }));
+ var m = new M({ x: [1,4,3,2] });
+ m.save(function (err) {
+ assert.ifError(err);
+ M.findById(m, function (err, m) {
+ assert.ifError(err);
+
+ assert.equal(1, m.x[0]);
+ assert.equal(4, m.x[1]);
+ assert.equal(3, m.x[2]);
+ assert.equal(2, m.x[3]);
+
+ m.x.sort();
+
+ m.save(function (err) {
+ assert.ifError(err);
+ M.findById(m, function (err, m) {
+ assert.ifError(err);
+
+ assert.equal(1, m.x[0]);
+ assert.equal(2, m.x[1]);
+ assert.equal(3, m.x[2]);
+ assert.equal(4, m.x[3]);
+
+ m.x.sort(function(a,b){
+ return b > a;
+ })
+
+ m.save(function (err) {
+ assert.ifError(err);
+ M.findById(m, function (err, m) {
+ assert.ifError(err);
+
+ assert.equal(4, m.x[0]);
+ assert.equal(3, m.x[1]);
+ assert.equal(2, m.x[2]);
+ assert.equal(1, m.x[3]);
+ done();
+ })
+ })
+ })
+ })
+ });
+ })
+ })
+ })
+
describe('setting a doc array', function(){
it('should adjust path positions', function(done){
var db = start();
Please sign in to comment.
Something went wrong with that request. Please try again.