How to group, remove duplicate, count, filter, foreach, Rename, remove field in MongoDb

Published: 04 December 2020
on channel: Rakiative
6,136
53

This video will help in understanding, How to group, remove duplicate, count, filter, foreach, Rename, remove field in MongoDb

mongo query Script : https://bit.ly/3utHAuy

Create a collection ("EmployeeDetail") and insert sample records
{ "Name" : "A", "Mobile" : "123" , "Salary" : 100 , "Department":[{"Name":"D1"}]}
{ "Name" : "B", "Mobile" : "456" , "Salary" : 200 , "Department":[{"Name":"D1"},{"Name":"D2"}], "Age" : 28}
{ "Name" : "C", "Mobile" : "789" , "Salary" : 300 , "Department":[{"Name":"D1"},{"Name":"D2"},{"Name":"D3"}]}
{ "Name" : "D", "Mobile" : "123" , "Salary" : 400 , "Department":null}
{ "Name" : "E", "Mobile" : "456" , "Salary" : 500 , "Department":[]}

Foreach
db.getCollection('EmployeeDetail').find({}).forEach(function (record) {
print("Name: " + record.Name)
});

// to avoid timeout use "noCursorTimeout" method when db has lots & lots of records

db.getCollection('EmployeeDetail').find({}).noCursorTimeout().forEach(function (record) {
print("Name: " + record.Name)
})

Convert objectId to string Id
db.getCollection('EmployeeDetail').find({}).forEach(function (record) {
var objectId = record._id;
record._id = record._id.valueOf();
if (typeof objectId === 'object') {
// duplicate same record with string type id
db.getCollection('EmployeeDetail').insert(record);
// remove old record with object type id
db.getCollection('EmployeeDetail').remove({
_id: objectId
});
}
})

Group example
db.getCollection('EmployeeDetail').aggregate(
[{
$group: {
// group by field name Mobile
_id: "$Mobile",
count: {
$sum: 1
}
}
}, {
$match: {
count: {
$gt: 1
}
}
}]
);

db.getCollection('EmployeeDetail').aggregate(
[{
$group: {
// group by field name Mobile
_id: "$Mobile",
// return additional field information of Name, Salary
Name: {
$addToSet: "$Name"
},
Salary: {
$addToSet: "$Salary"
},
count: {
$sum: 1
}
}
}, {
$match: {
count: {
$gt: 1
}
}
}]
);

Remove rows based on some duplicate field values
db.getCollection('EmployeeDetail').find({}, {
"Mobile": 1
}).sort({
_id: 1
}).forEach(function (record) {
db.getCollection('EmployeeDetail').remove({
_id: {
$gt: record._id
},
"Mobile": record.Mobile
});
});

Operators

db.getCollection('EmployeeDetail').find({
"Salary": {
$gt: 200
}
})

db.getCollection('EmployeeDetail').find({
"Salary": {
$gte: 200
}
})

db.getCollection('EmployeeDetail').find({
"Salary": {
$lt: 200
}
})

db.getCollection('EmployeeDetail').find({
"Salary": {
$lte: 200
}
})
// Return all where Department Name is not equal to D1
db.getCollection('EmployeeDetail').find({
"Department.Name": {
$ne: "D1"
}
})
Add Index
db.getCollection('EmployeeDetail').createIndex({
Name: 1
})
Rename Field
db.getCollection('EmployeeDetail').update({
}, {
$rename: {
Department: 'Dept'
}
}, false, true)
Update Row
// Add or update field
db.getCollection('EmployeeDetail').update({
"Name": "A"
}, {
$set: {
Age: NumberInt(18)
}
}, {
upsert: false,
multi: true
})
// Remove field
db.getCollection('EmployeeDetail').update({
"Mobile": "123"
}, {
$unset: {
Age: 1
}
}, {
upsert: false,
multi: true
})

Sort Rows
// Sort all the rows
db.getCollection('EmployeeDetail').find({}).sort({
$natural: -1
})
// Sort by salary field
db.getCollection('EmployeeDetail').find({}).sort({
"Salary": 1
})
Array Queries
// Return all where Department Name is D5
db.getCollection('EmployeeDetail').find({
"Department.Name": "D5"
})
// Return all where Department field is null
db.getCollection('EmployeeDetail').find({
Department: null
})
// Return all where Department array size is 0
db.getCollection('EmployeeDetail').find({
Department: {
$size: 0
}
});
// filter example
db.getCollection('EmployeeDetail').find({}).forEach(function (record) {
if (record.Department != null) {
var dept = record.Department.filter(function (de) {
return de.Name !== "D2";
});
record.Department = dept;

db.getCollection('EmployeeDetail').replaceOne({
_id: record._id
}, record);
} else {
}
});
Some basic query examples
// field exists or not
db.getCollection('EmployeeDetail').find({
"Age": {
$exists: false
}
})
// count example
var count = db.getCollection('EmployeeDetail').find({}).count();
if (count == 0) {
print("No Record");
} else {
print("Record count:" + count);
}

// skip and limit example
db.getCollection('EmployeeDetail').find({}).skip(1).limit(2)
// How to deal with multiple mongodb collection
db.getMongo().getDB("MongoQueryDemo").getCollection('EmployeeDetail').find({})


Watch video How to group, remove duplicate, count, filter, foreach, Rename, remove field in MongoDb online without registration, duration hours minute second in high quality. This video was added by user Rakiative 04 December 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 6,136 once and liked it 53 people.