Below is the sample data:
db.infos.find()
{
"groupId": "1111",
"customerId": "A100",
"tracks": [{
"trackId": "234",
"infos": [{
"location": {
"address": "street1",
"city": "test",
"country": "US"
}
},
{
"location": {
"address": "street2",
"city": "test",
"country": "US"
}
},
{
"location": {
"address": "street3",
"city": "test",
"country": "US"
}
}
]
}]
}
{
"groupId": "2222",
"customerId": "A100",
"tracks": [{
"trackId": "345",
"infos": [{
"location": {
"address": "street4",
"city": "test",
"country": "US"
}
},
{
"location": {
"address": "street5",
"city": "test",
"country": "US"
}
},
{
"location": {
"address": "street5",
"city": "test",
"country": "US"
}
}
]
}]
}
{
"groupId": "2222",
"customerId": "A100",
"tracks": [{
"trackId": "666",
"infos": [{
"location": {
"address": "street4",
"city": "test",
"country": "US"
}
},
{
"location": {
"address": "street5",
"city": "test",
"country": "US"
}
},
{
"location": {
"address": "street5",
"city": "test",
"country": "US"
}
}
]
}]
}
We need a query to get the length of "infos" sub array at groupId level. In the above sample data, we need the below output:
1111, 3
2222, 6
Tried below, but its not working:
db.infos.aggregate([{"$project": {"groupId": "$groupId", "samples": "$tracks.infos"}}, {"$group": {"_id": "$groupId", "samples": {"$push": "$samples"}}}, {"$project": {"_id": 1, "samples": {"$size": "$samples"}}}], { "allowDiskUse": true });
Also with large amount of data, above query is throwing an ExceededMemoryLimit exception:
2021-07-10T07:20:14.415+0000 E QUERY [js] Error: command failed: {
"ok" : 0,
"errmsg" : "$push used too much memory and cannot spill to disk. Memory limit: 104857600 bytes",
"code" : 146,
"codeName" : "ExceededMemoryLimit"
} : aggregate failed :
_getErrorWithCode#src/mongo/shell/utils.js:25:13
doassert#src/mongo/shell/assert.js:18:14
_assertCommandWorked#src/mongo/shell/assert.js:580:17
assert.commandWorked#src/mongo/shell/assert.js:673:16
DB.prototype._runAggregate#src/mongo/shell/db.js:260:9
DBCollection.prototype.aggregate#src/mongo/shell/collection.js:1074:12
#(shell):1:1
This should work with unwind:
db.collection.aggregate([
{
$unwind: "$tracks"
},
{
$group: {
"_id": "$groupId",
"count": {
"$sum": {
"$size": "$tracks.infos"
}
}
}
}
])
Even if your tracks array has more than one object this will add them up. Playground
Related
I planned to insert my code by MongoDB compass but when I clicked 'Insert' button, it only appears 'Inserting Document' but nothing happened even after I clicked the 'Insert' button many times and after that I try to refresh my MongoDB compass but nothing happened and the data is not inserted.
Here's my code:
{
"_id": "7",
"listing_id": "677728659232692395",
"arrival_date": new ISODate("2022-11-08T14:10:30Z"),
"departure_date": new ISODate("2022-11-15T14:10:30Z"),
"name": { first_name: "Gregorius", last_name: "Agung"
},
"email": "gregorius.agung04#gmail.com",
"contact": {
"daytime_phone": "0921970278",
"mobile": "0917820292",
"postal_address": {
"door": "5",
"floor": "12",
"street": "Swanston",
"suburb": "Weribee",
"state": "Victoria",
"country": "Australia",
"postal_code": "3003"
}, home_address: {
"door": "4",
"floor": "11",
"street": "Swanston",
"suburb": "Weribee",
"state": "Victoria",
"country": "Australia",
"postal_code": "3003"
}
},
"number_of_guests": 2,
"guests": [
{
"name": "Marvel",
"age": 22
},
{
"name": "Bryant",
"age": 23
}
],
"deposit_paid": {$numberDecimal: 120.01
},
"balance_due_amount": {$numberDecimal: 120.01
},
"balance_due_date": new ISODate(
"2022-11-08T14:10:30Z")
}
It seems your json is wrong. Some of the keys are not in quotes and for date insertion this is not working in compass.
I tried this and it is working
{
"_id": "7",
"listing_id": "677728659232692395",
"arrival_date": {
"$date": {
"$numberLong": "1667916630000"
}
},
"departure_date": {
"$date": {
"$numberLong": "1668521430000"
}
},
"name": {
"first_name": "Gregorius",
"last_name": "Agung"
},
"email": "gregorius.agung04#gmail.com",
"contact": {
"daytime_phone": "0921970278",
"mobile": "0917820292",
"postal_address": {
"door": "5",
"floor": "12",
"street": "Swanston",
"suburb": "Weribee",
"state": "Victoria",
"country": "Australia",
"postal_code": "3003"
},
"home_address": {
"door": "4",
"floor": "11",
"street": "Swanston",
"suburb": "Weribee",
"state": "Victoria",
"country": "Australia",
"postal_code": "3003"
}
},
"number_of_guests": 2,
"guests": [
{
"name": "Marvel",
"age": 22
},
{
"name": "Bryant",
"age": 23
}
],
"deposit_paid": {
"$numberDecimal": "120.01"
},
"balance_due_amount": {
"$numberDecimal": "120.01"
},
"balance_due_date": {
"$date": {
"$numberLong": "1667916630000"
}
}
}
I am trying to update the existing document with one extra field as a new requirement in DB needs that
Existing field
[{
"_id": {
"$oid": "62a8ad644035e93c7ff24607"
},
"user_name": "demo2#devia.com",
"password": "1234",
"college_id": {
"$oid": "628dfd41ef796e8f757a5c13"
},
"basic_details": {
"first_name": "ghgh",
"middle_name": "",
"last_name": "gh",
"email": "demo2#devia.com",
"mobile_number": 1234567890
},
"address_details": {
"country": {
"country_id": {
"$oid": "623012f9683f8fb7bd213c36"
},
"country_code": "IN"
},
"state": {
"state_id": {
"$oid": "623013d9683f8fb7bd2381c4"
},
"state_code": "MP"
},
"city": {
"city_id": {
"$oid": "6230139f683f8fb7bd21f1a9"
},
"city_name": "Akodia"
},
"address_line1": "",
"address_line2": "",
"pincode": ""
},
"is_verify": false,
"last_accessed": {
"$date": {
"$numberLong": "1655221604144"
}
},
"created_at": {
"$date": {
"$numberLong": "1655221604144"
}
},
"course_details": {}
}]
Needs to update all fields
[{
"_id": {
"$oid": "62bf2cf2a8c782741bbcc398"
},
"user_name": "a#exaple.com",
"password": "$2b$12$NgLNYm5jUBmFUq.w15.qCO35GiTJX09jAnOpoGuPt9G7GNuOkVN7K",
"college_id": {
"$oid": "628dfd41ef796e8f757a5c13"
},
"basic_details": {
"email": "a#example.com",
"mobile_number": "9898989898",
"first_name": "Fahim",
"middle_name": "",
"last_name": "Ashhab",
"nationality": "Antiguans",
"date_of_birth": "2003-02-04",
"admission_year": "2022-23",
"gender": "Male",
"category": "General",
"para_ability": {
"is_disable": false,
"name_of_disability": ""
}
},
"address_details": {
"communication_address": {
"country": {
"country_id": {
"$oid": "623012f9683f8fb7bd213c36"
},
"country_code": "IN"
},
"state": {
"state_id": {
"$oid": "623013d9683f8fb7bd2380ef"
},
"state_code": "AP"
},
"city": {
"city_id": {
"$oid": "6230139f683f8fb7bd21ebc8"
},
"city_name": "Akasahebpet"
},
"address_line1": "jjkjk",
"address_line2": "uiui",
"pincode": "989898"
}
},
"is_verify": true,
"last_accessed": {
"$date": {
"$numberLong": "1656696050670"
}
},
"created_at": {
"$date": {
"$numberLong": "1656696050670"
}
},
"allocate_to_counselor": {
"counselor_id": {
"$oid": "62bfd13a5ce8a398ad101bd7"
},
"counselor_name": "test",
"last_update": {
"$date": {
"$numberLong": "1656817948401"
}
}
},
"course_details": {
"BSc": {
"course_id": {
"$oid": "628e03d94e22276b98231407"
},
"course_name": "BSc",
"application_id": {
"$oid": "62bf2cf2a8c782741bbcc399"
},
"status": "Incomplete",
"specs": [
{
"spec_name": "Physician Assistant",
"is_activated": true
}
]
}
}
}]
Query Tried nothing is happening
db.collection.aggregate([
{$match:{"address_details.country": {$exists:true}}},
{$set:{address_details:{communication_address:"$address_details"}}}
])
2nd Query Tried didn't go through
db.studentsPrimaryDetails.updateMany({
"address_details.communication_address": {"$exist":false}},
{$set: {"address_details.communication_address":"$address_details"}})
The desired result needs to be
from this
"address_details": {
"country": {
"country_id": {
"$oid": "623012f9683f8fb7bd213c36"
},
"country_code": "IN"
},
"state": {
"state_id": {
"$oid": "623013d9683f8fb7bd2381c4"
},
"state_code": "MP"
},
"city": {
"city_id": {
"$oid": "6230139f683f8fb7bd21f1a9"
},
"city_name": "Akodia"
},
"address_line1": "",
"address_line2": "",
"pincode": ""
}
to this
"address_details": {
"communication_address": {
"country": {
"country_id": {
"$oid": "623012f9683f8fb7bd213c36"
},
"country_code": "IN"
},
"state": {
"state_id": {
"$oid": "623013d9683f8fb7bd2380ef"
},
"state_code": "AP"
},
"city": {
"city_id": {
"$oid": "6230139f683f8fb7bd21ebc8"
},
"city_name": "Akasahebpet"
},
"address_line1": "jjkjk",
"address_line2": "uiui",
"pincode": "989898"
}
}
Don't know what I am doing wrong.
Edit :
Desired full output
[{
"_id": {
"$oid": "62bf2cf2a8c782741bbcc398"
},
"user_name": "a#example.com",
"password": "12345678",
"college_id": {
"$oid": "628dfd41ef796e8f757a5c13"
},
"basic_details": {
"email": "a#example.com",
"mobile_number": "9898989898",
"first_name": "Fahim",
"middle_name": "",
"last_name": "Ashhab",
"nationality": "Antiguans",
"date_of_birth": "2003-02-04",
"admission_year": "2022-23",
"gender": "Male",
"category": "General",
"para_ability": {
"is_disable": false,
"name_of_disability": ""
}
},
"address_details": {
"communication_address": {
"country": {
"country_id": {
"$oid": "623012f9683f8fb7bd213c36"
},
"country_code": "IN"
},
"state": {
"state_id": {
"$oid": "623013d9683f8fb7bd2380ef"
},
"state_code": "AP"
},
"city": {
"city_id": {
"$oid": "6230139f683f8fb7bd21ebc8"
},
"city_name": "Akasahebpet"
},
"address_line1": "jjkjk",
"address_line2": "uiui",
"pincode": "989898"
}
},
"is_verify": true,
"last_accessed": {
"$date": {
"$numberLong": "1656696050670"
}
},
"created_at": {
"$date": {
"$numberLong": "1656696050670"
}
},
"allocate_to_counselor": {
"counselor_id": {
"$oid": "62bfd13a5ce8a398ad101bd7"
},
"counselor_name": "viru chaudhary",
"last_update": {
"$date": {
"$numberLong": "1656817948401"
}
}
},
"course_details": {
"BSc": {
"course_id": {
"$oid": "628e03d94e22276b98231407"
},
"course_name": "BSc",
"application_id": {
"$oid": "62bf2cf2a8c782741bbcc399"
},
"status": "Incomplete",
"specs": [
{
"spec_name": "Physician Assistant",
"is_activated": true
}
]
}
}
}]```
One option is:
Use $exists instead of $exist
Use pipeline in order to be able to $set the value from another fields' value
db.collection.updateMany(
{"address_details.communication_address": {$exists: false}},
[{$set: {address_detailsB: {communication_address: "$address_details"}}},
{$set: {
address_details: "$address_detailsB",
address_detailsB: "$$REMOVE"}
}
])
See how it works on the playground example
The aggregation query that you first tried, does not not update the db, it only returns data. In order to use it as an updater you need to add a $merge step.
How to get the nested object in projection in mongodb find query.
[
{
"apikey": 1,
"meta": {
"region": {
"country": "India",
"city": "bangalore",
"pincode": 560067
},
"address": {
"houseNo": "G/C 42 Whitefield boulavourd",
"landmark": "whitefield boulavourd"
}
}
},
{
"apikey": 2,
"meta": {
"region": {
"country": "Germaany",
"city": "Munich",
"pincode": 80297
},
"address": {
"houseNo": "Zweibrückenstraße 12",
"landmark": "Zweibrückenstraße 12"
}
}
}
]
I was trying to fetch the region of apikey 2. I tried below find query
I tried find query i.e.
db.collection.find({
"apikey": "2"
},
{
"projection": {
"_id": 0,
"apikey": 1,
"meta.region": 1
}
})
I am getting error, regarding that can not do inclusion on field meta.region in exclusion projection.
Is there any other way to achieve this problem.
I want the output,
[
{
"apikey":2,
"region": {
"country": "Germaany",
"city": "Munich",
"pincode": 80297
}
}
]
This is the mongoplayground
Remove projection (wrapping) level.
db.collection.find({
apikey: 2
},
{
"_id": 1,
"apikey": "$apikey",
"region": "$meta.region"
})
Sample Mongo Playground
I have two collections one is Employee and the other one is a salary. I want to make a calculated field in employee collection call monthly salary. How can I access a SALARY from Salary collection and divided by 12?
Here are some info I included in MongoDB:
db.Salary.insertMany([
{
"POSITION": "1",
"BRANCH_SIZE": "HQ",
"SALARY": "150000"
},
{
"POSITION": "2",
"BRANCH_SIZE": "HQ",
"SALARY": "100000"
},
{
"POSITION": "3",
"BRANCH_SIZE": "HQ",
"SALARY": "70000"
},
{
"POSITION": "4",
"BRANCH_SIZE": "HQ",
"SALARY": "30000"
},
{
"POSITION": "5",
"BRANCH_SIZE": "HQ",
"SALARY": "56000"
}
])
db.Employee.insertMany([
{
"EMPLOYEE_NO": "1000",
"LNAME": "Wyatt",
"FNAME": " Stefan"
"CITY": "MANKATO",
"STATE": "MN",
"ZIP": "56001",
"STATUS": "1",
"POSITION": "1"
},
{
"EMPLOYEE_NO": "1029",
"LNAME": "Martin",
"FNAME": "Edward",
"STATUS": "1",
"START_DATE": "02-MAY-95",
"END_DATE": "",
"BRANCH_NO": "103",
"BRANCH_SIZE": "MD",
"POSITION": "3"
},
{
"EMPLOYEE_NO": "1089",
"LNAME": "Stewart",
"FNAME": "Macy",
"CITY": "SAINT PAUL",
"BRANCH_NO": "101",
"BRANCH_SIZE": "BG",
"POSITION": "4"
}
])
I want to make a monthly salary for each employee. How can I do that?
Thanks in advance.
Any help would be greatly appreciated.
You can use $lookup and $divide operator to grab salary from salary collection, and divide one of the fields by 12.
Example:
db.employee.aggregate([{
$lookup: {
from: 'salary',
localField: 'POSITION',
foreignField: 'POSITION',
as: 'salary',
},
},
{
{
$project: {
dividedSalary: {
$divide: ["$salary.SALARY", 12]
}
}
}
}
])
Index not picking when using N1QL to match all the customers belonging to a country
Note : the bucket has around 10 lack records, the document is fetching after 50 seconds,
query
select * from `user-prof` WHERE ANY item IN devices.location SATISFIES item.country IN ["US"]
index
CREATE INDEX id_userProfile ON `user-prof` ( ALL ARRAY v.country FOR v IN devices.location END )
Document
[
{
"customers": {
"devices": {
"user": "u1",
"custid": "14CE5534CCE",
"token": "4D5BE85896833148D696A1397C",
"guest": {
"lastActive": null,
"searches": [
]
},
"latt": "6655059908",
"locale": "en-US",
"location": [
{
"city": "Afc",
"country": "IN"
},
{
"city": "Newyork",
"country": "US"
},
{
"city": null,
"country": null
}
],
"long": "4.21787927806",
"notify": {
"Stats": false
},
"tier": null,
"tmz": "EU",
"version": "0.1"
}
}
},
{
"customers": {
"devices": {
"user": "u2",
"custid": "64CE5534CC1E",
"token": "6D5BE85896833148D696A1397C",
"guest": {
"lastActive": null,
"searches": [
]
},
"latt": "6655059908",
"locale": "en-US",
"location": [
{
"city": "Texas",
"country": "US"
},
{
"city": null,
"country": null
}
],
"long": "4.21787927806",
"notify": {
"Stats": false
},
"tier": null,
"tmz": "EU",
"version": "0.1"
}
}
}
You are using Pre 4.6.2 The variable in the ANY clause needs to be same as the variable in the CREATE INDEX (i.e item ,v )
https://developer.couchbase.com/documentation/server/current/n1ql/n1ql-language-reference/indexing-arrays.html