Found 1349 Articles for MongoDB

How to use $slice operator to get last element of array in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

710 Views

To get the last element of array in MongoDB, use the following syntaxdb.yourCollectionName.find({}, {yourArrayFieldName:{$slice:-1}});Let us first create a collection with documents>db.getLastElementOfArrayDemo.insertOne({"StudentName":"James", "StudentMathScore":[78, 68, 98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2d71a629b87623db1b2e") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Chris", "StudentMathScore":[88, 56, 34]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2d83a629b87623db1b2f") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Larry", "StudentMathScore":[99]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2d8ea629b87623db1b30") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Robert", "StudentMathScore":[90, 78, 67, 66, 75, 73]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2dada629b87623db1b31") }Following is the query to display all documents from a collection with the help of find() method> db.getLastElementOfArrayDemo.find().pretty();This will produce the following output{   ... Read More

Can I retrieve multiple documents from MongoDB by id?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

2K+ Views

Yes, to retrieve multiple docs from MongoDB by id, use the $in operator. The syntax is as followsdb.yourCollectionName.find({_id:{$in:[yourValue1, yourValue2, yourValue3, ...N]}});Let us first create a collection with documents:> db.retrieveMultipleDocsByIdDemo.insertOne({"_id":10, "CustomerName":"John"}); { "acknowledged" : true, "insertedId" : 10 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":14, "CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : 14 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":20, "CustomerName":"Robert"}); { "acknowledged" : true, "insertedId" : 20 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":25, "CustomerName":"Sam"}); { "acknowledged" : true, "insertedId" : 25 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":30, "CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : 30 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":34, "CustomerName":"Carol"}); { "acknowledged" : true, "insertedId" : 34 }Following is the query to display all documents ... Read More

Insert to specific index for MongoDB array?

George John
Updated on 30-Jul-2019 22:30:25

434 Views

To insert a specific index for MongoDB array, you can use $push operator. Let us create a collection with documents>db.insertToSpecificIndexDemo.insertOne({"StudentName":"Larry", "StudentSubjects":["MySQL", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2562a629b87623db1b2c") } >db.insertToSpecificIndexDemo.insertOne({"StudentName":"Chris", "StudentSubjects":["C++", "C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2573a629b87623db1b2d") }Following is the query to display all documents from a collection with the help of find() method> db.insertToSpecificIndexDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9d2562a629b87623db1b2c"),    "StudentName" : "Larry",    "StudentSubjects" : [       "MySQL",       "Java"    ] } {    "_id" : ObjectId("5c9d2573a629b87623db1b2d"),    "StudentName" : "Chris",   ... Read More

MongoDB query which represents not equal to null or empty?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

7K+ Views

To set a query for not equal to null or empty, use the $nin operator. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$nin:[null, ""]}});Let us create a collection with documents> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Larry", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20b6a629b87623db1b26") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"", "UserAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20bea629b87623db1b27") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Sam", "UserAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20c7a629b87623db1b28") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":null, "UserAge":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20d2a629b87623db1b29") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Robert", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20dda629b87623db1b2a") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"", "UserAge":23}); {    "acknowledged" : true,   ... Read More

How can I use MongoDB to find all documents which have a field, regardless of the value of that field?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

70 Views

To use MongoDB to find all documents which have a field, regardless of the value of that field, use the $exists operator. Following is the syntaxdb.yourCollectionName.find({yourFieldName:{$exists:true}});Let us create a collection with documents>db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"John", "StudentAge":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d60a629b87623db1b22") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Larry", "StudentAge":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d70a629b87623db1b23") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Chris", "StudentAge":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d7ba629b87623db1b24") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Robert", "StudentAge":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d81a629b87623db1b25") }Following is the query to display all documents from a collection with the help of find() method> db.findAllDocumentWhichHaveFieldDemo.find().pretty();This will produce the following ... Read More

Inserting Date() in MongoDB through Mongo shell?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

In order to insert Date() in MongoDB through Mongo shell, use the following syntaxvar yourVariableName= new Date(year, month, day, hour, minute); db.yourCollectionName({yourDateFieldName:yourVariableName});Let us first create a date variable> var creatingDate = new Date(2019, 03, 29, 13, 12);Let us create a collection with documents:>db.insertingDateUsingVariableDemo.insertOne({"UserName":"John", "UserMessages":["Hi", "Hello", "Awesome"], "UserPostDate":creatingDate});Following is the query to display all documents from a collection with the help of find() method> db.insertingDateUsingVariableDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9d1b19a629b87623db1b21"),    "UserName" : "John",    "UserMessages" : [       "Hi",       "Hello",       "Awesome"    ],    "UserPostDate" : ISODate("2019-04-29T07:42:00Z") }Read More

Removing all collections whose name matches a string in MongoDB

George John
Updated on 30-Jul-2019 22:30:25

442 Views

To remove all collections whose name matches a string, you can follow some steps. Use for loop to iterate over all collections and find that particular collection name with certain string. After that, use the drop method to remove all collections.Let’s say we are using the database “sample”. The collections are as follows in sample database> show collections;This will produce the following outputarraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteMultipleIdsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findMimimumElementInArrayDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo insertDocumentWithDateDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformationNow remove all the collection names that match a ... Read More

How to delete multiple ids in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

1K+ Views

To delete multiple ids in MongoDB, you can use $in operator. Following is the syntaxdb.yourCollectionName.remove( { _id : { $in: [yourObjectId1, yourObjectId2, yourObjectId3)] } } );Let us create a collection with documents> db.deleteMultipleIdsDemo.insertOne({"ClientName":"Chris", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7d6a629b87623db1b19") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Robert", "ClientAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7dea629b87623db1b1a") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Sam", "ClientAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7e9a629b87623db1b1b") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"John", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd7f7a629b87623db1b1c") } > db.deleteMultipleIdsDemo.insertOne({"ClientName":"Carol", "ClientAge":36}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cd803a629b87623db1b1d") }Following is the query to ... Read More

How to insert a document with date in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

2K+ Views

To insert a document with date in MongoDB, use Date(). Following is the syntax“yourFieldName”:new Date(yourDateValue);Let us create a collection with documents. Following is the query>db.insertDocumentWithDateDemo.insertOne({"UserName":"Larry", "UserMessage":"Hi", "UserMessagePostDate":new Date("2012-09-24")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cca58a629b87623db1b16") } >db.insertDocumentWithDateDemo.insertOne({"UserName":"Chris", "UserMessage":"Hello", "UserMessagePostDate":new Date("2015-12-31")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cca71a629b87623db1b17") } >db.insertDocumentWithDateDemo.insertOne({"UserName":"Robert", "UserMessage":"Bye", "UserMessagePostDate":new Date("2019-01-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9cca85a629b87623db1b18") }Following is the query to display all documents from a collection with the help of find() method> db.insertDocumentWithDateDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9cca58a629b87623db1b16"),    "UserName" : "Larry",    "UserMessage" : "Hi", ... Read More

How to connect to my MongoDB table by command line?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

152 Views

In order to connect to my table by command line, you need to use db commanddb.yourCollectionName.find();Let’s say we have a database “sample” with some collections. First check the current database> use sample; switched to db sample > db; Sample Now we have reached the database sample. The database “sample” is having the following collections: > show collections;This will produce the following outputarraySizeErrorDemo basicInformationDemo copyThisCollectionToSampleDatabaseDemo deleteAllRecordsDemo deleteDocuments deleteDocumentsDemo deleteSomeInformation documentWithAParticularFieldValueDemo employee findListOfIdsDemo findSubstring getAllRecordsFromSourceCollectionDemo getElementWithMaxIdDemo internalArraySizeDemo largestDocumentDemo makingStudentInformationClone oppositeAddToSetDemo prettyDemo returnOnlyUniqueValuesDemo selectWhereInDemo sourceCollection studentInformation sumOfValueDemo sumTwoFieldsDemo truncateDemo updateInformation userInformationHere is the correct way of connecting to a table i.e. collection. You ... Read More

Advertisements