Database Articles

Page 476 of 546

Return True if a document exists in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 4K+ Views

Let us first create a collection. Following is the query to create a collection with documents> db.documentExistsOrNotDemo.insertOne({"UserId":101, "UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9932bd330fd0aa0d2fe4cf") } > db.documentExistsOrNotDemo.insertOne({"UserId":102, "UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9932c6330fd0aa0d2fe4d0") } > db.documentExistsOrNotDemo.insertOne({"UserId":102, "UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9932ce330fd0aa0d2fe4d1") }Following is the query to display all the documents from a collection with the help of find() method> db.documentExistsOrNotDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9932bd330fd0aa0d2fe4cf"),    "UserId" : 101,    "UserName" : "John" } {    "_id" : ObjectId("5c9932c6330fd0aa0d2fe4d0"),    "UserId" : 102,   ...

Read More

Can I get the first item in a Cursor object in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 718 Views

Yes, you can get the first item in a cursor object using findOne() method. Following is the syntaxdb.yourCollectionName.findOne();However, the following syntax is used if you want a single document in a cursor objectdb.yourCollectionName.findOne({yourCondition});We will first create a collection. Following is the query to create a collection with documents> db.getFirstItemDemo.insertOne({"CustomerName":"Chris", "CustomerAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c989059330fd0aa0d2fe4c1") } > db.getFirstItemDemo.insertOne({"CustomerName":"Larry", "CustomerAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c989063330fd0aa0d2fe4c2") } > db.getFirstItemDemo.insertOne({"CustomerName":"Robert", "CustomerAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c98906d330fd0aa0d2fe4c3") } > db.getFirstItemDemo.insertOne({"CustomerName":"David", "CustomerAge":39}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c989081330fd0aa0d2fe4c4") }Following is ...

Read More

Drop all indexes from all the collections in a MongoDB database using the command line?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 296 Views

Following is the syntax to drop all indexes from all collections in a MongoDB database using command linedb.getCollectionNames().forEach(function(yourVariableName) {    db.runCommand({dropIndexes: yourVariableName, index: "*"}); });The above syntax will drop all indexes except _id.Let us check the current database. Following is the query> dbThis will produce the following outputTestFollowing is the query to let us show some indexes from a collection before dropping indexes> db.indexingDemo.getIndexes();This will produce the following output[    {       "v" : 2,       "key" : {          "_id" : 1       },       "name" : "_id_", ...

Read More

How to get all the collections from all the MongoDB databases?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 586 Views

To get all the collections from all the databases, let us first get all the databases using the following query> switchDatabaseAdmin = db.getSiblingDB("admin"); admin > allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases;This will produce the following output[    {       "name" : "admin",       "sizeOnDisk" : 495616,       "empty" : false    },    {       "name" : "config",       "sizeOnDisk" : 98304,       "empty" : false    },    {       "name" : "local",       "sizeOnDisk" : 73728,       "empty" : false ...

Read More

Search for a text in MongoDBs Double Nested Array?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 166 Views

Search for a text in MongoDBs Double Nested Array with the help of dot(.) notation. Let us first create a collection. Following is the query to create a collection with documents> db.doubleNestedArrayDemo.insertOne( ...    { ...       "StudentId" : "1000", ...       "StudentName" : "Larry", ...       "StudentDetails" : [ ...          { ...             "ProjectName" : "Online Banking", ...             "ProjectDetails" : [ ...                { ...               ...

Read More

Get all the MongoDB documents but not the ones with two given criteria’s?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 142 Views

To get all the MongoDB documents with some given criteria’s, following any of the below given casesCase 1 Following is the query to get all the documents without a single criterion using $ne operatordb.yourCollectionName.find({yourFieldName:{$ne:"yourValue"}}).pretty();Case 2 Following is the query to get all the documents without two given criterions using $nin operatordb.yourCollectionName.find({yourFieldName:{$nin:["yourValue1", "yourValue2"]}}).pretty();Let us first create a collection. Following is the query to create a collection with documents>db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Larry", "StudentSubjectName":"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c993d82330fd0aa0d2fe4d2") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Chris", "StudentSubjectName":"C++"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c993d8f330fd0aa0d2fe4d3") } >db.findAllExceptFromOneOrtwoDemo.insertOne({"StudentName":"Robert", "StudentSubjectName":"C"}); { "acknowledged" : true, "insertedId" : ObjectId("5c993d99330fd0aa0d2fe4d4") } ...

Read More

Search multiple fields for multiple values in MongoDB?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 765 Views

To search multiple fields for multiple values in MongoDB, you can use $text and $search operator. Let us first create a collection with documents>db.searchMultipleFieldsDemo.insertOne({"_id":100, "FirstSubject":"Java", "SecondSubject":"MongoDB"}); { "acknowledged" : true, "insertedId" : 100 } >db.searchMultipleFieldsDemo.insertOne({"_id":101, "FirstSubject":"MongoDB", "SecondSubject":"MySQL"}); { "acknowledged" : true, "insertedId" : 101 } >db.searchMultipleFieldsDemo.insertOne({"_id":102, "FirstSubject":"MySQL", "SecondSubject":"Java"}); { "acknowledged" : true, "insertedId" : 102 }Following is the query to display all documents from a collection with the help of find() method> db.searchMultipleFieldsDemo.find().pretty();This will produce the following output{ "_id" : 100, "FirstSubject" : "Java", "SecondSubject" : "MongoDB" } { "_id" : 101, "FirstSubject" : "MongoDB", "SecondSubject" : "MySQL" } { ...

Read More

How to create an index with MongoDB?

George John
George John
Updated on 30-Jul-2019 210 Views

To create an index in MongoDB, use the ensureIndex() method. Let us first create a collection using following query> db.createCollection(&qu/ot;creatingUniqueIndexDemo"); { "ok" : 1 }Following is the query to create an index on the above collection:> db.creatingUniqueIndexDemo.ensureIndex({"UserCountryName":1}, {unique:true}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Following is the query to insert some documents in the above collection>db.creatingUniqueIndexDemo.insertOne({"UserName":"John", "UserAge":21, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9947bd330fd0aa0d2fe4d8") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Mike", "UserAge":23, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9947c9330fd0aa0d2fe4d9") } >db.creatingUniqueIndexDemo.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"US"}); 2019-03-26T02:57:52.670+0530 E QUERY [js] WriteError: ...

Read More

How can I to know if my database MongoDB is 64 bits?

George John
George John
Updated on 30-Jul-2019 166 Views

You can use buidInfo along with runCommand to check MongoDB for 32 bits or 64 bits. First switch your database to admin. Following is the syntaxuse adminAfter that use the following syntax to know if my server runs MongoDB 64 bits or notdb.runCommand(buildInfo)Now execute the above syntax> use admin switched to db admin > db.runCommand("buildInfo");Following is the output displaying MongoDB is 64 bits{    "version" : "4.0.5",    "gitVersion" : "3739429dd92b92d1b0ab120911a23d50bf03c412",    "targetMinOS" : "Windows 7/Windows Server 2008 R2",    "modules" : [ ],    "allocator" : "tcmalloc",    "javascriptEngine" : "mozjs",    "sysInfo" : "deprecated",    "versionArray" : [ ...

Read More

Find all the non-distinct values of a field in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 1K+ Views

Use aggregate() method to get all the non-distinct values of a field. Let us first create a collection with documents> db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995078863d6ffd454bb647") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995081863d6ffd454bb648") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995089863d6ffd454bb649") } > db.findAllNonDistinctDemo.insertOne({"UserName":"David", "UserAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c995093863d6ffd454bb64a") } > db.findAllNonDistinctDemo.insertOne({"UserName":"John", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c99509d863d6ffd454bb64b") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9950a7863d6ffd454bb64c") } > db.findAllNonDistinctDemo.insertOne({"UserName":"Robert", "UserAge":25}); ...

Read More
Showing 4751–4760 of 5,456 articles
« Prev 1 474 475 476 477 478 546 Next »
Advertisements