Found 1349 Articles for MongoDB

How to remove document on the basis of _id in MongoDB?

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

150 Views

To remove document on the basis of _id in MongoDB, implement the following syntaxdb.yourCollectionName.remove({“_id”:ObjectId(“yourId”});Let us first implement the following query to create a collection with documents>db.removeDocumentOnBasisOfId.insertOne({"UserName":"Larry", "UserAge":23, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986f9f330fd0aa0d2fe4a3") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Sam", "UserAge":21, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fb4330fd0aa0d2fe4a4") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Chris", "UserAge":24, "UserCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fc0330fd0aa0d2fe4a5") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"Robert", "UserAge":26, "UserCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fcf330fd0aa0d2fe4a6") } >db.removeDocumentOnBasisOfId.insertOne({"UserName":"David", "UserAge":28, "UserCountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986fed330fd0aa0d2fe4a7") }Following is the query to display all documents from a collection ... Read More

Increment a value in a MongoDB nested object?

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

369 Views

To increment a value in nested object, you can use $inc operator. Let us first implement the following query to create a collection with documents>db.incrementValueDemo.insertOne({"StudentName":"Larry", "StudentCountryName":"US", "StudentDetails":[{"StudentSubjectName":"Math", "StudentMathMarks":79}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c986ca0330fd0aa0d2fe4a2") }Following is the query to display all documents from a collection with the help of find() method> db.incrementValueDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c986ca0330fd0aa0d2fe4a2"),    "StudentName" : "Larry",    "StudentCountryName" : "US",    "StudentDetails" : [       {          "StudentSubjectName" : "Math",          "StudentMathMarks" : 79       }    ] ... Read More

Get the duplicate values of a field in MongoDB?

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

1K+ Views

Use aggregate() method to get the duplicate value of a field. Let us first create a collection with documents using the following query> 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") } ... Read More

Inserting the current datetime in MongoDB?

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

3K+ Views

To insert current datetime in MongoDB, use the $setOnInsert operator. Let us first implement the following query to create a collection with documents>db.addCurrentDateTimeDemo.insertOne({"StudentName":"John", "StudentAdmissionDate":new Date("2012-01-21") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae45330fd0aa0d2fe49f") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2013-05-24") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae54330fd0aa0d2fe4a0") } >db.addCurrentDateTimeDemo.insertOne({"StudentName":"Carol", "StudentAdmissionDate":new Date("2019-07-26") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97ae5f330fd0aa0d2fe4a1") }Following is the query to display all documents from a collection with the help of find() method> db.addCurrentDateTimeDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c97ae45330fd0aa0d2fe49f"),    "StudentName" : "John",    "StudentAdmissionDate" : ISODate("2012-01-21T00:00:00Z") } { ... Read More

How to search document in MongoDB by _id

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

351 Views

To search a document in MongoDB by _id, you need to call ObjectId(). Let us first see the syntaxdb.yourCollectionName.find({"_id":ObjectId("yourId")}).pretty();To understand the concept and search a document, let us implement the following query to create a collection with documents> db.searchDocumentDemo.insertOne({"UserId":1, "UserName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8e4330fd0aa0d2fe487") } > db.searchDocumentDemo.insertOne({"UserId":2, "UserName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8ea330fd0aa0d2fe488") } > db.searchDocumentDemo.insertOne({"UserId":3, "UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8f1330fd0aa0d2fe489") } > db.searchDocumentDemo.insertOne({"UserId":4, "UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c97a8fa330fd0aa0d2fe48a") } > db.searchDocumentDemo.insertOne({"UserId":5, "UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ... Read More

How to create double nested array in MongoDB?

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

274 Views

To create a double nested array in MongoDB, let us implement the query to create a collection with documents. Within that, we have created a double nested array that displays Student Details, with the project name and technologies used to develop the same project:> db.doubleNestedArrayDemo.insertOne( ... { ...    "StudentId" : "1000", ...    "StudentName" : "Larry", ...    "StudentDetails" : [ ...    { ...       "ProjectName" : "Online Banking", ...       "ProjectDetails" : [ ...       { ...          "TechnologyUsed" : "Java" ...       }, ...   ... Read More

Get the array of _id in MongoDB?

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

638 Views

The _id in MongoDB is a field, which is mandatory. In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. Following is the syntax to get the array of all the ids i.e. _id in MongoDBdb.yourCollectionName.find({ _id : { $in : [yourValue1, yourValue2, yourValue3, .......N] } } );Let us first implement the following query to create a collection with documents> db.selectInWhereIdDemo.insertOne({"_id":23}); { "acknowledged" : true, "insertedId" : 23 } > db.selectInWhereIdDemo.insertOne({"_id":28}); { "acknowledged" : true, "insertedId" : 28 } > db.selectInWhereIdDemo.insertOne({"_id":45}); { "acknowledged" : true, "insertedId" : 45 } > db.selectInWhereIdDemo.insertOne({"_id":75}); ... Read More

Get all the column names in a table in MongoDB

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

6K+ Views

In MongoDB, there is no concept of columns since MongoDB is schema-less and does not contain tables. It contains the concept of collections and a collection has different types of documents to store items.Let us see the syntax −db.yourCollectionName.insertOne({“YourFieldName”:yourValue, “yourFieldName”:”yourValue”, .......N});If you want a single record from a collection, you can use findOne() and in order to get all records from the collection, you can use find().The syntax is as follows −db.yourCollectionName.findOne(); //Get Single Record db.yourCollectionName.find(); // Get All RecordTo understand the above syntax, let us create a collection with the document. The query to create a collection with a ... Read More

How do I insert a record from one Mongo database into another?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

435 Views

You can switch from one database to another using the use command. Here, we are using the collection in the “test” database. Let us insert that collection in another database with the name “sample”.To understand further, let us create a collection with the document. The query to create a collection with a document is as follows −> db.insertOneRecordDemo.insertOne({"UserName":"Larry", "UserAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9534de16f542d757e2b452") } > db.insertOneRecordDemo.insertOne({"UserName":"Chris", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9534e816f542d757e2b453") } > db.insertOneRecordDemo.insertOne({"UserName":"David", "UserAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9534f116f542d757e2b454") }Display all documents from a collection ... Read More

How does MongoDB order their docs in one collection?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

50 Views

MongoDB order the docs in one collection with the help of a $natural operator. It stores the document as it is when we get from find(). The default order is $natural. Let us now see the syntax −db.yourCollectionName.find().sort({ "$natural": 1 });To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.orderDocsDemo.insertOne({"UserScore":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9531a316f542d757e2b44b") } > db.orderDocsDemo.insertOne({"UserScore":98}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9531a816f542d757e2b44c") } > db.orderDocsDemo.insertOne({"UserScore":99}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9531b216f542d757e2b44d") ... Read More

Advertisements