Found 6705 Articles for Database

Export specified field of a collection in mongodb / mongodump to file?

AmitDiwan
Updated on 31-Mar-2020 13:33:26

507 Views

To export MongoDB has a command mongoexport. Following is the syntax −mongoexport -d yourDatabaseName -c yourCollectionName -f yourFieldName --type=csv -o yourFileLocation/FileName;Let us create a collection with documents −> db.demo284.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4abc9e9127fafea82a2cfc") } > db.demo284.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4abca39127fafea82a2cfd") } > db.demo284.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4abca79127fafea82a2cfe") }Display all documents from a collection with the help of find() method −> db.demo284.find();This will produce the following output −{ "_id" : ObjectId("5e4abc9e9127fafea82a2cfc"), "FirstName" : "Chris" } { "_id" : ObjectId("5e4abca39127fafea82a2cfd"), "FirstName" : "Robert" } { "_id" : ... Read More

MongoDB query to update array with another field?

AmitDiwan
Updated on 31-Mar-2020 13:31:09

355 Views

To update array with another field, use $push. Let us create a collection with documents −> db.demo283.insertOne({"Name":"Chris", "Status":["Active", "Inactive"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4ab97ddd099650a5401a75") }Display all documents from a collection with the help of find() method −> db.demo283.find();This will produce the following output −{ "_id" : ObjectId("5e4ab97ddd099650a5401a75"), "Name" : "Chris", "Status" : [ "Active", "Inactive" ] }Following is the query to update array with another field −> db.demo283.update({}, {$push: {Status:{$each:['Regular', 'NotRegular'], '$slice': -4}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> ... Read More

Retrieving specific documents from collection by _id in MongoDB

AmitDiwan
Updated on 31-Mar-2020 13:24:53

180 Views

To retrieve document from collection by _id, use find() with $in. Let us create a collection with documents −> db.demo281.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4aac28dd099650a5401a66") } > db.demo281.insertOne({"Name":"Bob", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4aac46dd099650a5401a67") } > db.demo281.insertOne({"Name":"David", "Age":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e4aac4fdd099650a5401a68") }Display all documents from a collection with the help of find() method −> db.demo281.find();This will produce the following output −{ "_id" : ObjectId("5e4aac28dd099650a5401a66"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e4aac46dd099650a5401a67"), "Name" : "Bob", "Age" : 23 } { "_id" : ... Read More

How to delete element from an array in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:22:05

278 Views

To delete element from an array, use $pull. Let us create a collection with documents −> db.demo279.insertOne({id:[101, 103, 105, 110]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e490af7dd099650a5401a58") } > db.demo279.insertOne({id:[107, 111, 110]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e490b06dd099650a5401a59") }Display all documents from a collection with the help of find() method −> db.demo279.find();This will produce the following output −{ "_id" : ObjectId("5e490af7dd099650a5401a58"), "id" : [ 101, 103, 105, 110 ] } { "_id" : ObjectId("5e490b06dd099650a5401a59"), "id" : [ 107, 111, 110 ] }Following is the query to delete element from an array &minus';> db.demo279.update({}, {$pull:{id:110}}, {multi:true}); ... Read More

How to create a performance system that count tags across a large dynamic dataset in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:20:59

97 Views

Create an index and then use explain(). Let us create a collection with documents −> db.demo278.ensureIndex({"Subjects":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo278.insertOne({"Subjects":["MySQL", "MongoDB", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e49096edd099650a5401a55") } > db.demo278.insertOne({"Subjects":["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e490978dd099650a5401a56") } > db.demo278.insertOne({"Subjects":["Spring", "Hibernate"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e490984dd099650a5401a57") }Display all documents from a collection with the help of find() method −> db.demo278.find();This will produce the following output −{ "_id" : ObjectId("5e49096edd099650a5401a55"), "Subjects" : [ "MySQL", ... Read More

Multi-key Indexing on an entire array with MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:16:35

107 Views

Let us first create a collection with documents −> db.demo277.insertOne({"details":[{"FirstName":"John"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48fb21dd099650a5401a52") } > db.demo277.insertOne({"details":[{"FirstName":"David"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48fb27dd099650a5401a53") } > db.demo277.insertOne({"details":[{"FirstName":"Chris"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48fb2bdd099650a5401a54") }Display all documents from a collection with the help of find() method −> db.demo277.find();This will produce the following output −{ "_id" : ObjectId("5e48fb21dd099650a5401a52"), "details" : [ { "FirstName" : "John" } ] } { "_id" : ObjectId("5e48fb27dd099650a5401a53"), "details" : [ { "FirstName" : "David" } ] } { "_id" : ObjectId("5e48fb2bdd099650a5401a54"), "details" : [ { "FirstName" : ... Read More

Split a document by its subdocuments in MongoDB

AmitDiwan
Updated on 31-Mar-2020 13:14:32

613 Views

To split a document by its subdocuments, use $unwind in MongoDB. Let us create a collection with documents −> db.demo276.insertOne({"Name":"Chris", "Subjects":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48f953dd099650a5401a51") }Display all documents from a collection with the help of find() method −> db.demo276.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e48f953dd099650a5401a51"),    "Name" : "Chris",    "Subjects" : [       "MySQL",       "MongoDB"    ] }Following is the query to split a document by its subdocuments −> db.demo276.aggregate( [ { $unwind : "$Subjects" } ] )This will produce the following output −{ ... Read More

MongoDB query to skip first 5 records and display only the last 5 of them

AmitDiwan
Updated on 31-Mar-2020 13:12:34

2K+ Views

To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit().Let us create a collection with documents −> db.demo275.insertOne({"Number":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eac4dd099650a5401a43") } > db.demo275.insertOne({"Number":12}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eac7dd099650a5401a44") } > db.demo275.insertOne({"Number":6}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eac9dd099650a5401a45") } > db.demo275.insertOne({"Number":1}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eacadd099650a5401a46") } > db.demo275.insertOne({"Number":5}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48eacddd099650a5401a47") } > db.demo275.insertOne({"Number":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48ead0dd099650a5401a48") } > db.demo275.insertOne({"Number":8}); { ... Read More

Can I utilize indexes when querying by MongoDB subdocument without known field names?

AmitDiwan
Updated on 31-Mar-2020 13:10:51

111 Views

Yes, you can achieve this by indexing like “properties.k” for key and “properties.v” for value. The same is used to be implemented in ensureIndex().Let us first see an example and create a collection with documents −> db.demo274.insertOne({"details":[{StudentFirstName:"Chris", StudentLastName:"Brown"}, ...   {StudentFirstName:"David", StudentLastName:"Miller"}, ...   {StudentFirstName:"John", StudentLastName:"Smith"}, ...   {StudentFirstName:"John", StudentLastName:"Doe"} ...] ...} ...); {    "acknowledged" : true,    "insertedId" : ObjectId("5e48de35dd099650a5401a42") }Display all documents from a collection with the help of find() method −> db.demo274.find().pretty();OutputThis will produce the following output −{    "_id" : ObjectId("5e48de35dd099650a5401a42"),    "details" : [       {          "StudentFirstName" : ... Read More

How to insert LONG numbers in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 13:07:14

2K+ Views

To insert LONG numbers, use NumberLong(). It is used to handle 64-bit integers. Let us create a collection with documents −> db.demo273.insert({ ...   Name:"Robert", ...   id: NumberLong("100000000000001"), ...   isActive:true ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ...   Name:"David", ...   id: NumberLong("98888888888888888"), ...   isActive:false ...}) WriteResult({ "nInserted" : 1 }) > db.demo273.insert({ ...   Name:"Mike", ...   id: NumberLong("999999999999"), ...   isActive:true ...}) WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo273.find();OutputThis will produce the following output −{ "_id" : ObjectId("5e4824df1627c0c63e7dbac3"), "Name" : "Robert", "id" ... Read More

Advertisements