My data structure is:
{
"_id": { "$oid" : "511D0A0EC075F3FF25000003" },
"progresses": [
{
"behavior": {
"behavior": {
"_id": "511d052f52fbf0fd25000002",
"disabled": false,
"name": "Unity Installed",
"key": "UnityInstalled",
"points": 1,
"timeout": 0
},
"key": "UnityNotInstalled",
"name": "Unity Not Installed",
"points": 1,
"timeout": 0,
"disabled": false,
"_id": { "$oid" : "511D056552FBF0FD25000003" }
},
"behaviorCount": 1,
"behaviorParameter": null,
"userId": null,
"modifiedAt": { "$date": 1360857614000.000000 },
"createdAt": { "$date": 1360857614000.000000 },
"behaviorType": "user"
},
{
"behavior": {
"behavior": {
"_id": "511cfac4955737a01f000001",
"disabled": false,
"name": "test",
"key": "tt",
"points": 1,
"timeout": 0
},
"name": "Unity Installed",
"key": "UnityInstalled",
"points": 1,
"timeout": 0,
"disabled": false,
"_id": { "$oid" : "511D052F52FBF0FD25000002" }
},
"behaviorCount": 3,
"behaviorParameter": null,
"userId": null,
"modifiedAt": { "$date": 1360858565000.000000 },
"createdAt": { "$date": 1360858553000.000000 },
"behaviorType": "user"
},
{
"behavior": {
"behavior": {
"_id": "511d052f52fbf0fd25000002",
"disabled": false,
"name": "Unity Installed",
"key": "UnityInstalled",
"points": 1,
"timeout": 0
},
"name": "Active User",
"key": "ActiveUser",
"points": 1,
"timeout": 0,
"disabled": false,
"_id": { "$oid" : "511D058E52FBF0FD25000004" }
},
"behaviorCount": 1,
"behaviorParameter": null,
"userId": null,
"modifiedAt": { "$date": 1360858565000.000000 },
"createdAt": { "$date": 1360858565000.000000 },
"behaviorType": "user"
},
{
"behavior": {
"behavior": {
"_id": "511d058e52fbf0fd25000004",
"disabled": false,
"name": "Active User",
"key": "ActiveUser",
"points": 1,
"timeout": 0
},
"name": "Invite Count",
"key": "InviteCount",
"points": 1,
"timeout": 0,
"disabled": false,
"_id": { "$oid" : "511D061752FBF0FD25000006" }
},
"behaviorCount": 1,
"behaviorParameter": null,
"userId": null,
"modifiedAt": { "$date": 1360858587000.000000 },
"createdAt": { "$date": 1360858587000.000000 },
"behaviorType": "user"
}
],
"trophyAchievements": [
],
"userId": "asfasd"
}
I want to update all progresses.behaviorCount to 1. How can i done that ?
You should use mongo's positional operator and dot notation.
In your case, I'd use smth like this:
db.test.update({}, {$set: {"progresses.$.behaviorCount": "testing"}}, { multi: true })
But, it won't update all fields in the nested array, just one at a time.
See similar problems:
MongoDB: Updating subdocument
Updating a sub-document in mongodb?
As a workaround, you can loop over nested array and update behaviorCount one at a time by index.
Hope that helps.
This script is the answer:
var i = 0;
db.users.find().forEach(function(doc) {
doc.progresses.forEach(function(progress) {
progress.behaviorCount = 1;
});
db.users.save(doc);
if (i % 10000 == 0) print(i);
i++;
});
Related
I have this Mongo DB document structure...
{
"_id": {
"$oid": "63b6e12d650c56ca2e720c86"
},
"userName": "Harshit Gupta ",
"email": "harshitguptapositive#gmail.com",
"password": "$2b$10$Ys0AfxbtQKX3XytM1n85dO0dUL2bFAMQiD3w5nSw6zFmda4W4yRn6",
"tasks": [
{
"habbitName": "Jogging",
"Description": "defended",
"Sunday": false,
"Monday": true,
"Tuesday": false,
"Wednesday": true,
"Thursday": false,
"Friday": true,
"Saturday": false,
"Month": [
{
"date": 0,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c8d"
}
},
{
"date": 1,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c8e"
}
},
{
"date": 2,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c8f"
}
},
{
"date": 3,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c90"
}
},
{
"date": 4,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c91"
}
},
{
"date": 5,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c92"
}
},
{
"date": 6,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c93"
}
},
{
"date": 7,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c94"
}
},
{
"date": 8,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c95"
}
},
{
"date": 9,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c96"
}
},
{
"date": 10,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c97"
}
},
{
"date": 11,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c98"
}
},
{
"date": 12,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c99"
}
},
{
"date": 13,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c9a"
}
},
{
"date": 14,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c9b"
}
},
{
"date": 15,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c9c"
}
},
{
"date": 16,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c9d"
}
},
{
"date": 17,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c9e"
}
},
{
"date": 18,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720c9f"
}
},
{
"date": 19,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca0"
}
},
{
"date": 20,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca1"
}
},
{
"date": 21,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca2"
}
},
{
"date": 22,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca3"
}
},
{
"date": 23,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca4"
}
},
{
"date": 24,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca5"
}
},
{
"date": 25,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca6"
}
},
{
"date": 26,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca7"
}
},
{
"date": 27,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca8"
}
},
{
"date": 28,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720ca9"
}
},
{
"date": 29,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720caa"
}
},
{
"date": 30,
"done": false,
"_id": {
"$oid": "63b6e14c650c56ca2e720cab"
}
}
],
"timeRemind": "23:12",
"_id": {
"$oid": "63b6e14c650c56ca2e720c8c"
},
"createdAt": {
"$date": {
"$numberLong": "1672929612245"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1672929612245"
}
}
}
}
],
"createdAt": {
"$date": {
"$numberLong": "1672929581689"
}
},
"updatedAt": {
"$date": {
"$numberLong": "1672939914562"
}
},
"__v": 2
}
I want to first select user id then specific the task id and the document which gets selected then
I want to update specific date of the month field can you please suggest the specific query..
first select select user by user id then a task by task id then in Month field i want to update a n element(done attribute to true) whose date is 4.
I tried to apply the following operation.
let doc = await User.findOneAndUpdate({
_id: req.user.id
}
,{
"tasks.$[task].Month.$[dateIndex].done": true
},{
arrayFilters:[ {"dateIndex.date" : 4} ,{"task.id":req.params.taskId}]
}
);
The positional parameter $ only matches the first element of the top-most array.
To update within an object in an array nested in another array, use arrayFilters and the $[] positional filter to refer into the nested arrays, like:
User.findOneAndUpdate({
_id: mongoose.Types.ObjectId(req.user.id),
"tasks._id": mongoose.Types.ObjectId(req.params.taskId)
},
{$set: {"tasks.$.Month.$[m].done": true}},
{
arrayFilters: [{"m.date": dateIndice}]
})
Playground
When using unwind("items") each item produces a duplicate line. This leads to revenue being counted as many times as there are items.
Example: Someone buys 1 item of A and 1 item of B, resulting in a cummulative value of 10. Unwind now inserts a row for each item, leading to a sum(cummulative) of 20.
I tried grouping the elements directly after unwind but have not managed to get it to work properly.
How can I sum each array element without duplication every other value?
I have this data structure
{
"_id": {
"$binary": "VEE6CsjHPvjzS2JYso7mnQ==",
"$type": "3"
},
"sequentialId": {
"$numberLong": "1"
},
"date": "2022-02-04",
"invoiceTotal": {
"$numberDecimal": "9.85"
},
"vatTotal": {
"$numberDecimal": "0"
},
"vatPercentage": {
"$numberDecimal": "19.00"
},
"invoiceNumber": "1111111",
"type": "ELEKTRONISCH",
"aktivkonto": 2800,
"passivkonto": 5200,
"buyerEmail": "",
"username": "",
"shop": "",
"externalId": "",
"shipped": false,
"actualShippingCost": {
"$numberDecimal": "1"
},
"filename": "",
"isReported": false,
"deliveryCostTotal": {
"$numberDecimal": "4.35"
},
"items": [
{
"lineItemId": "",
"amount": "1",
"sku": "A123123",
"title": "",
"priceTotal": {
"$numberDecimal": "4.50"
},
"vatTotal": {
"$numberDecimal": "0"
},
"hardwareCostPerPiece": {
"$numberDecimal": "0.22"
},
"hardwareCostTotal": {
"$numberDecimal": "0.22"
}
},
{
"lineItemId": "",
"amount": "1",
"sku": "B212312",
"title": "",
"priceTotal": {
"$numberDecimal": "1.00"
},
"vatTotal": {
"$numberDecimal": "0"
},
"hardwareCostPerPiece": {
"$numberDecimal": "0.22"
},
"hardwareCostTotal": {
"$numberDecimal": "0.22"
}
}
],
"packagingCost": {
"$numberDecimal": "0.15"
},
"hasInvoiceSent": false,
"tenant": "you!",
"createdAt": {
"$date": "2022-02-04T15:23:40.716Z"
},
"modifiedAt": {
"$date": "2022-02-04T15:23:40.716Z"
},
"_class": "_.RevenueEntity"
}
and this query
fun sumAllByWeeklyAndTenant(tenant: String): Flux<DashboardRevenue> {
val aggregation = newAggregation(
match(findByTenant(tenant)),
unwind("items"),
group("invoiceNumber")
.sum("items.hardwareCostTotal").`as`("hardwareCostTotal"),
project()
.andExpression("year(createdAt)").`as`("year")
.andExpression("week(createdAt)").`as`("week")
.andInclude(bind("hardwareCostTotal", "items.hardwareCostTotal"))
.andInclude(
"invoiceTotal",
"vatTotal",
"actualShippingCost",
"packagingCost",
"marketplaceFeesTotal",
"tenant"
),
group("year", "week", "tenant")
.sum("invoiceTotal").`as`("umsatz")
.sum("actualShippingCost").`as`("portokosten")
.sum("packagingCost").`as`("verpackung")
.sum("marketplaceFeesTotal").`as`("marketplaceFees")
.sum("hardwareCostTotal").`as`("hardwareCost")
.sum("vatTotal").`as`("vatTotal")
.count().`as`("numberOfInvoices"),
sort(Sort.Direction.DESC, "year", "week"),
limit(8),
sort(Sort.Direction.ASC, "year", "week")
)
return reactiveMongoTemplate
.aggregate(aggregation, "revenue", DashboardRevenue::class.java)
.toFlux()
}
Using the data above the query results in
[
{
"_id": {
"year": 2022,
"month": 2,
"week": 0,
"tenant": "einsupershop"
},
"umsatz": 19.70,
"portokosten": 2,
"verpackung": 0.30,
"marketplaceFees": 3.42,
"hardwareCost": 0.22,
"vatTotal": 0,
"numberOfInvoices": 2
}
]
Where the expected value is "invoiceTotal": { "$numberDecimal": "9.85" }
Using mongoose-delete https://www.npmjs.com/package/mongoose-delete
I am gettings this error
E11000 duplicate key error index: hose_api.takeoffs.$horse_1_competition_1_event_1_deletedAt_1 dup key: { : ObjectId('5dd5dcb0f1dbcd08d0dda8ca'), : ObjectId('5f89f86444a69c11583086dd'), : ObjectId('5f89ed7504266450e81ea5ef'), : new Date(1602881879322) }"}
Every time i try to soft delete the document.
This is the index that i am using:
{ "horse": 1, "competition": 1, "event": 1, "deletedAt": 1 }
document example:
{
"_id": {
"$oid": "5f8a086a414a1a5d941557e0"
},
"entry": 0,
"hasPass": false,
"pass": 3,
"order": 999999,
"deleted": true,
"competitor": {
"$oid": "5e5f1a9e020c37154079fc15"
},
"competition": {
"$oid": "5f89f86444a69c11583086dd"
},
"horse": {
"$oid": "5dd5dcb0f1dbcd08d0dda8ca"
},
"payment": "NONE",
"event": {
"$oid": "5f89ed7504266450e81ea5ef"
},
"owner": {
"$oid": "5e056556b3b6192584c1f7a2"
},
"updatedAt": {
"$date": "2020-10-16T20:57:59.322Z"
},
"createdAt": {
"$date": "2020-10-16T20:54:02.268Z"
},
"__v": 0,
"deletedAt": {
"$date": "2020-10-16T20:57:59.322Z"
},
"deletedBy": null
}
I have my json object like this
{
"_id": "5c2e811154855c0012308f00",
"__pclass": "QXRzXFByb2plY3RcTW9kZWxcUHJvamVjdA==",
"id": 44328,
"name": "Test project via postman2//2",
"address": "some random address",
"area": null,
"bidDate": null,
"building": {
"name": "Health Care Facilities",
"type": "Dental Clinic"
},
"collaborators": [],
"createdBy": {
"user": {
"id": 7662036,
"name": "Someone Here"
},
"firm": {
"id": 2520967,
"type": "ATS"
}
},
"createdDate": "2019-01-03T21:39:29Z",
"customers": [],
"doneBy": null,
"file": null,
"firm": {
"id": 1,
"name": "MyFirm"
},
"leadSource": {
"name": "dontknow",
"number": "93794497"
},
"location": {
"id": null,
"city": {
"id": 567,
"name": "Bahamas"
},
"country": {
"id": 38,
"name": "Canada"
},
"province": {
"id": 7,
"name": "British Columbia"
}
},
"modifiedBy": null,
"modifiedDate": null,
"projectPhase": {
"id": 1,
"name": "pre-design"
},
"quotes": [{
"id": 19,
"opportunityValues": {
"Key1": 100,
"Key2 Key2": 100,
"Key3 Key3 Key3": 200,
}
}],
"specForecast": [],
"specIds": [],
"tags": [],
"valuation": "something"
}
I am trying to aggregate using this query in MongoDB. My aggregation key is 4 level deep and also contains spaces. On all online examples shows me the aggregation at the first level. Looking to the online codes, I tried to re-iterate the same with my 4th level deep key.
db.mydata.aggregate([
{$match: {"id": 44328 } } ,
{$group: { _id: "$quotes.id",
totalKey2:{ $sum: "$quotes.opportunityValues.Key2 Key2"},
totalKey3:{ $sum: "$quotes.opportunityValues.Key3 Key3 Key3"}
}
}
]);
This should return
_id totalKey2 totalKey3
0 19 100 300
But it is returning
_id totalKey2 totalKey3
0 19 0 0
What am I doing Wrong?
Although it's not recommended to use space in field names in Mongo, it works as expected.
The problem with your query is that "quotes" is an array and you should first unwind it before grouping it.
This works as expected:
db.mydata.aggregate([
{ $match: { "id": 44328 } } ,
{ $unwind: "$quotes" },
{ $group: { _id: "$quotes.id",
totalKey2:{ $sum: "$quotes.opportunityValues.Key2 Key2" },
totalKey3:{ $sum: "$quotes.opportunityValues.Key3 Key3 Key3" } }
}
]);
I have this document in my mongodb collection:
{
"_id": "YLRM9Wi7f6tp6qNbS",
"sessionId": "hLDkkJKR4Muik6tbe",
"userId": "ZYoG4cH8HcCDPMDGr",
"shopId": "J8Bhq3uTtdgwZx3rz",
"workflow": {
"status": "",
"workflow": ["String"]
},
"billing": [Object],
"discount": 0,
"tax": 0,
"items": [
{
"_id": "JwR233jD2c4HKeYKq",
"shopId": "J8Bhq3uTtdgwZx3rz",
"productId": "BCTMZ6HTxFSppJESk",
"quantity": 1,
"product": {
"_id": "BCTMZ6HTxFSppJESk",
"title": "Product",
"shopId": "J8Bhq3uTtdgwZx3rz",
"ancestors": [],
"createdAt": "2018-01-12T10:22:18.853Z",
"description": "",
"handle": "product",
"hashtags": [
"rpjCvTBGjhBi2xdro",
"cseCBSSrJ3t8HQSNP"
],
"price": {
"range": "12.99 - 19.99",
"min": 12.99,
"max": 19.99
},
"isVisible": true,
"isLowQuantity": false,
"isSoldOut": false,
"isBackorder": false,
"metafields": [
{
"key": "Material",
"value": "Cotton"
},
{
"key": "Quality",
"value": "Excellent"
}
],
"pageTitle": "",
"type": "simple",
"updatedAt": "2018-01-12T10:22:18.854Z",
"vendor": "Vendor_Name",
"originCountry": "country",
"requiresShipping": true,
"isDeleted": false,
"template": "productDetailSimple",
"workflow": {
"status": "new"
}
},
"variants": {},
"title": "Product",
"type": "simple",
"parcel": {
"weight": 25,
"height": 3,
"width": 10,
"length": 10
},
"shippingMethod": {
"shopId": "J8Bhq3uTtdgwZx3rz",
"shipmentQuotes": [Object],
"shipmentQuotesQueryStatus": {
"requestStatus": "success",
"numOfShippingMethodsFound": 11
},
"_id": "s3EJXrLsZe73RbLiD",
"address": {},
"shipmentMethod": {},
"paymentId": "nyybR5BNvDDrJrtwe",
"items": [
{
"_id": "JwR233jD2c4HKeYKq",
"productId": "BCTMZ6HTxFSppJESk",
"shopId": "J8Bhq3uTtdgwZx3rz",
"variantId": "CJoRBm9vRrorc9mxZ"
}
],
"workflow": {
"status": "new",
"workflow": ["String"]
}
},
"workflow": {
"status": "new",
"workflow": ["String"]
}
}
],
"shipping": [Object],
"email": "johndoe#mail.com",
"cartId": "L6sSGv4NR9rpbDbsd",
"createdAt": "2018-01-12T10:22:18.850Z"
}
The field items is an array of objects, I would like to update just a part of the object specifically the workflow field without touching other part of the objects in items array.
I was able to do this using a loop, but it caused some tests to fail. Is there a better of doing this with using a loop?
Thank you.
You can try findAndModify method.
Traverse to the workflow key ad try to set the value.
Hope this would help.