Skip to content

Commit

Permalink
Merge branch '6.2' of github.com:Automattic/mongoose into 6.2
Browse files Browse the repository at this point in the history
  • Loading branch information
vkarpov15 committed Jan 9, 2022
2 parents 93a3fca + 7c31dc8 commit ff42635
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 30 deletions.
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -969,11 +969,11 @@ declare module 'mongoose' {
estimatedDocumentCount(options?: QueryOptions, callback?: Callback<number>): QueryWithHelpers<number, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;

/**
* Returns true if at least one document exists in the database that matches
* the given `filter`, and false otherwise.
* Returns a document with its `_id` if at least one document exists in the database that matches
* the given `filter`, and `null` otherwise.
*/
exists(filter: FilterQuery<T>): Promise<boolean>;
exists(filter: FilterQuery<T>, callback: Callback<boolean>): void;
exists(filter: FilterQuery<T>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
exists(filter: FilterQuery<T>, callback: Callback<Pick<Document<T>, '_id'> | null>): QueryWithHelpers<Pick<Document<T>, '_id'> | null, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;

/** Creates a `find` query: gets a list of documents that match `filter`. */
find(callback?: Callback<HydratedDocument<T, TMethods, TVirtuals>[]>): QueryWithHelpers<Array<HydratedDocument<T, TMethods, TVirtuals>>, HydratedDocument<T, TMethods, TVirtuals>, TQueryHelpers, T>;
Expand Down
8 changes: 2 additions & 6 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1051,8 +1051,8 @@ Model.prototype.model = function model(name) {
};

/**
* Returns true if at least one document exists in the database that matches
* the given `filter`, and false otherwise.
* Returns a document with `_id` only if at least one document exists in the database that matches
* the given `filter`, and `null` otherwise.
*
* Under the hood, `MyModel.exists({ answer: 42 })` is equivalent to
* `MyModel.findOne({ answer: 42 }).select({ _id: 1 }).lean()`
Expand Down Expand Up @@ -1089,10 +1089,6 @@ Model.exists = function exists(filter, options, callback) {
if (typeof callback === 'function') {
return query.exec(callback);
}
options = options || {};
if (!options.explain) {
return query.then(doc => !!doc);
}

return query;
};
Expand Down
55 changes: 35 additions & 20 deletions test/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6668,36 +6668,47 @@ describe('Model', function() {
}, /Model\.discriminator.*MyModel/);
});

describe('exists() (gh-6872)', function() {
it('returns true if document exists', function() {
describe('exists() (gh-6872) (gh-8097) (gh-11138)', function() {
it('returns a query', () => {
const User = db.model('Test', new Schema({ name: String }));
const query = User.exists({ name: 'Hafez' });
assert.ok(query instanceof mongoose.Query);
});

it('returns lean document with `_id` only if document exists', async function() {
const Model = db.model('Test', new Schema({ name: String }));

return Model.create({ name: 'foo' }).
then(() => Model.exists({ name: 'foo' })).
then(res => assert.ok(res)).
then(() => Model.exists({})).
then(res => assert.ok(res)).
then(() => Model.exists()).
then(res => assert.ok(res));
const docFromCreation = await Model.create({ name: 'foo' });
const existingDocument = await Model.exists({ _id: docFromCreation._id });
assert.equal(existingDocument._id.toString(), docFromCreation._id.toString());
assert.deepStrictEqual(existingDocument, { _id: docFromCreation._id });
assert.ok(isLean(existingDocument));
});

it('returns false if no doc exists', function() {

it('returns `null` when no document exists', async() => {
const Model = db.model('Test', new Schema({ name: String }));

return Model.create({ name: 'foo' }).
then(() => Model.exists({ name: 'bar' })).
then(res => assert.ok(!res)).
then(() => Model.exists({ otherProp: 'foo' }, { strict: false })).
then(res => assert.ok(!res));
const existingDocument = await Model.exists({ name: 'I do not exist' });
assert.equal(existingDocument, null);
});
it('returns `null` if no doc exists', async function() {
const Model = db.model('Test', new Schema({ name: String }));

await Model.create({ name: 'foo' });

it('options (gh-8075)', function() {
const existingDocumentWithStrict = await Model.exists({ otherProp: 'foo' }, { strict: false });
assert.equal(existingDocumentWithStrict, null);
});

it('options (gh-8075)', async function() {
const Model = db.model('Test', new Schema({ name: String }));

return Model.exists({}).
then(res => assert.ok(!res)).
then(() => Model.exists({}, { explain: true })).
then(res => assert.ok(res));
const existingDocument = await Model.exists({});
assert.equal(existingDocument, null);

const explainResult = await Model.exists({}, { explain: true });
assert.ok(explainResult);
});
});

Expand Down Expand Up @@ -7653,4 +7664,8 @@ describe('Model', function() {

async function delay(ms) {
await new Promise((resolve) => setTimeout(resolve, ms));
}

function isLean(document) {
return document != null && !(document instanceof mongoose.Document);
}
8 changes: 8 additions & 0 deletions test/typescript/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,11 @@ Project.create({
}).then(project => {
project.myMethod();
});


Project.exists({ name: 'Hello' }).then(result => {
result?._id;
});
Project.exists({ name: 'Hello' }, (err, result) => {
result?._id;
});

0 comments on commit ff42635

Please sign in to comment.