You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Note: only mongodb.uri is mandatory, you can always override/set database and collection names in each cypress
mongodb command using options. You can set both local and remote urls.
Finally, in your cypress/support/e2e.js add the following:
cy.createCollection('someCollection');// collection with name `someCollection` will be createdcy.createCollection('someOtherCollection',{database: 'someDatabase',failSilently: 'true'}).then(result=>{cy.log(result);// Will return 'Collection created' or the error object if collection already exists. Will not fail the test});cy.dropCollection('someCollection');// collection will be dropedcy.dropCollection('nonexistentCollection',{database: 'someDatabase',failSilently: 'true'}).then(result=>{cy.log(result);// Will return 'Collection dropped' or the error object if collection doesn’t exist. Will not fail the test});
An array of Document objects that will be inserted
options
Object (optional)
Provide additional options (see below)
> examples
cy.insertOne({document: 1});// will insert the provided document in mongodbcy.insertOne({document: 1},{collection: 'someCollection',database: 'someDatabase'}).then(result=>{cy.log(result);// prints the _id of inserted document});cy.insertMany([{document: 1},{document: 2}]);// will insert provided documents in mongodbcy.insertMany([{document: 1},{document: 2}],{collection: 'some_other_collection'}).then(result=>{console.log(result);// prints the key-value pairs of the inserted ids});
Specifies query selection criteria using query operators
filter
Object (required)
The selection criteria for the deletion
options
Object (optional)
Provide additional options (see below)
> examples
import{ObjectId}from'mongodb';cy.findOne({_id: newObjectId()}).then(result=>{cy.log(result);// prints the document with the _id if found, otherwise null});cy.findMany({document: 1}).then(result=>{cy.log(result);// prints the array of documents if any matched, or empty array});cy.findOneAndUpdate({document: 2},{$set: {document: 3}}).then(result=>{cy.log(result);// prints the original document with value 2});cy.findOneAndUpdate({document: 3},{$set: {document: 4}},{upsert: true,returnDocument: 'after'}).then((result: any)=>{cy.log(result);// prints the updated document with the value 4, will create (upsert) a new document if none are found});
cy.deleteOne({document: 1});// will delete a first matched documentcy.deleteOne({document: 1},{collection: 'new_collection',database: 'some_database'}).then(result=>{cy.log(result);// prints 1 (or 0) document deleted});cy.deleteMany(deleteClause).then(res=>{cy.log(result);// prints '# documents deleted'});
A document representing the mongodb command to run
options
Object (optional)
Provide additional options (see below)
> examples
constcommand={listCollections: 1,nameOnly: true};// any kind of commandcy.runCommand(command).then(result=>{cy.log(result);// prints the result of the command});
> available options
Options
Default
Description
database
Value specified in the mongodb environment variable
Database on top of which the command will be executed
collection
Value specified in the mongodb environment variable
Collection on top of which the command will be executed
failSilently
false
Control if the command will fail or if the collection is not found