Found 1349 Articles for MongoDB

Retrieve only the queried element in an object array in MongoDB collection?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

236 Views

You can use projection operator $elemMatch to filter in queried element in an object array in MongoDB collection. To retrieve only the queried element in an object array in MongoDB, let us first create a collection with documents object array.The query is as follows:> db.objectArray.insert({"Persons":[    {"PersonName":"Adam", "PersonSalary":25000}, {"PersonName":"Larry", "PersonSalary":27000    }]});    WriteResult({ "nInserted" : 1 }) > db.objectArray.insert({"Persons":[    {"PersonName":"David", "PersonSalary":32000}, {"PersonName":"Carol", "PersonSalary":77000    }]});    WriteResult({ "nInserted" : 1 })Now you can display all the documents with the help of find(). The query is as follows:> db.objectArray.find().pretty();The following is the output:{    "_id" : ObjectId("5c6bfadc68174aae23f5ef53"),    "Persons" ... Read More

How to retrieve documents from a collection in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

1K+ Views

To retrieve documents from a collection in MongoDB, you need to use find() method. The syntax is as follows:db.yourCollectionName.find();The above syntax will return all the documents from a collection in MongoDB. To understand the above syntax, let us create a collection with documents. The query to create documents are as follows:> db.retrieveAllStudents.insertOne({"StudentId":"STUD101", "StudentName":"David", "StudentAge":24}); {    "acknowledged" : true, "insertedId" : ObjectId("5c6bf5cf68174aae23f5ef4e") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD102", "StudentName":"Carol", "StudentAge":22}); {    "acknowledged" : true, "insertedId" : ObjectId("5c6bf5e968174aae23f5ef4f") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD103", "StudentName":"Maxwell", "StudentAge":25}); {    "acknowledged" : true, "insertedId" : ObjectId("5c6bf5f768174aae23f5ef50") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD104", "StudentName":"Bob", "StudentAge":23}); {    "acknowledged" : true, "insertedId" : ... Read More

How to delete all the documents from a collection in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25

555 Views

If you want to delete all documents from the collection, you can use deleteMany(). Let us first create a collection and insert some documents to it:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })Now display all the documents from the collection. The query is as follows:> db.deleteDocumentsDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ab0e064f3d70fcc914805"),    "Name" : "Larry",    "Age" : 23 } {    "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"),    "Name" : "Mike",    "Age" : 21 } {    "_id" : ObjectId("5c6ab0f864f3d70fcc914807"),    "Name" ... Read More

How to delete document from a collection in MongoDB using deleteOne() method?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

236 Views

To delete document from a collection in MongoDB, you can use the deleteOne() method. Let us first create a collection and insert some documents to it:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })Now display all the documents from the collection. The query is as follows:> db.deleteDocumentsDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ab0e064f3d70fcc914805"),    "Name" : "Larry",    "Age" : 23 } {    "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"),    "Name" : "Mike",    "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" : "Sam", ... Read More

How to delete documents from a collection in MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

338 Views

To delete the documents from a collection in MongoDB, you need to use remove() method. The syntax is as follows:db.yourCollectionName.remove(yourDeleteValue);Here, let us create a collection with some documents. The query is as follows:>db.deleteDocuments.insert({"UserId":1, "UserName":"Bob", "UserTechnicalSubject":"Introducti on to PL/SQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":2, "UserName":"Carol", "UserTechnicalSubject":"Introduction to MongoDB"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":3, "UserName":"John", "UserTechnicalSubject":"Introduction to MySQL"}); WriteResult({ "nInserted" : 1 }) >db.deleteDocuments.insert({"UserId":4, "UserName":"Maxwell", "UserTechnicalSubject":"Introduction to SQL Server"}); WriteResult({ "nInserted" : 1 })You can display all documents from the collection we created above with the help of find() command. The query is as follows:> db.deleteDocuments.find().pretty();The following ... Read More

How to insert new documents into a MongoDB collection in your database?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25

232 Views

To insert new documents into a MongoDB collection, you need to use insert() method or save() method.Case 1: Using insert() method.The syntax is as follows:db.yourCollectionName.insert(yourDocument);Case 2: Using save() method.The syntax is as follows:db.yourCollectionName.save(yourDocument);In the above syntax, if your collection name does not exist then MongoDB will create a new collection and insert the document in the collection.The query to insert new documents is as follows for both the cases discussed above.Case 1: Using insert() method. The query is as follows:> db.userInformation.insert({    ... "Name":"John",    ... Age:30,    ... isStudent:false,    ... "Subjects":["Introduction to java", "Introduction to MongoDB"]    ... ... Read More

Open Source Databases

Alex Onsman
Updated on 20-Jun-2020 08:17:31

1K+ Views

Open source databases are those databases who have an open source code i.e anyone may view the code, study it or even modify it. Open source databases could be relational (SQL) or non relational (NoSQL).Why use Open Source Databases?It is quite expensive to create and maintain a database for any company. A huge chunk of the total software expenditure is used to handle databases. So, it is feasible to switch to low cost open source databases. This saves companies a lot of money in the long run.Open Source Databases in useThere many different open source databases in the market. All ... Read More

How to insert a Python object in Mongodb?

Rajendra Dharmkar
Updated on 16-Jun-2020 07:36:08

566 Views

You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. For example, Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use ... Read More

How to prepare a Python date object to be inserted into MongoDB?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:39:05

1K+ Views

You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo.Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use datetime.datetime.utcnow(), which returns ... Read More

Advertisements