How to update a part of an array sub document in MongoDB - mongodb

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.

Related

Pymongo "and" operator not filtering query results [duplicate]

This question already has answers here:
Retrieve only the queried element in an object array in MongoDB collection
(18 answers)
Closed 25 days ago.
I have this monogdb schema:
groups = {
"bsonType": "object",
"required": [
"created_at", "group_name", "owner",
"members", "currency", "country",
"group_username"
],
"properties": {
"created_at": {
"bsonType": "date",
"description": "The date and time when the group was created"
},
"group_name": {
"bsonType": "string",
"description": "Name of group"
},
"group_username": {
"bsonType": "string",
"description": "groups username"
},
"country": {
"bsonType": "string",
"description": "User's country and is required"
},
"currency": {
"bsonType": "string",
"description": "User's currency type and is required"
},
"description": {
"bsonType": "string",
"description": "Brief description of what the group does"
},
"block": {
"bsonType": "bool",
"description": "Field to check if user disabled from group activity"
},
"owner": {
"bsonType": "object",
"required": ["user_id", "status"],
"properties": {
"user_id": {
"bsonType": "objectId",
"description": "User id of the owner of the group"
},
"status": {
"bsonType": "string",
"description": "Default value is 'owner'",
}
},
},
"group_invite_link": {
"bsonType": "string",
"description": "Link used to invite new members"
},
"members": {
"bsonType": "array",
"items": {
"bsonType": "object",
"required": ["user_id", "name", "status", "confirmed", "joined_at"],
"properties": {
"user_id": {
"bsonType": "objectId"
},
"name": {
"bsonType": "string",
"description": "Name of group member"
},
"status": {
"enum": ["admin", "member"],
"description": "can only be one of the enum values and is required"
},
"confirmed": {
"bsonType": "bool",
"description": "Confirms if user is fully authorized to operate in group"
},
"joined_at": {
"bsonType": "date",
"description": "Date user joined group"
}
}
}
},
"group_wallet_id": {
"bsonType": "objectId",
"description": "Id of wallet associated with group"
}
}
}
and I am querying it to return only documents that match the below query:
db.groups.find({
"$and": [
{
"members.user_id": ObjectId(user_id)
},
{
"members.confirmed": True
}
]
})
It returns this:
{
"sucess": true,
"message": "Users groups",
"data": [
{
"_id": "63987c4263d7eba1e79a1df1",
"created_at": "2022-12-13 13:21:03",
"group_name": "Demo 1",
"is_owner": true,
"balance": {
"$numberDecimal": "250.00"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd8",
"name": "Joy Kudosen",
"status": "member",
"confirmed": true,
"joined_at": "2022-12-14 13:22:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
},
{
"_id": "63987c5a63d7eba1e79a1df4",
"created_at": "2022-12-13 13:21:27",
"group_name": "Demo 2",
"is_owner": true,
"balance": {
"$numberDecimal": "100"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:27"
}
]
},
{
"_id": "6399ae06ebaca8cd5fbcd639",
"created_at": "2022-12-14 11:05:39",
"group_name": "Demo 4",
"is_owner": true,
"balance": {
"$numberDecimal": "0"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
},
{
"_id": "63d1810699edcb7379f21c27",
"created_at": "2023-01-25 19:20:33",
"group_name": "Hello world",
"is_owner": false,
"balance": {
"$numberDecimal": "0"
},
"members": [
{
"user_id": "63d180c399edcb7379f21c23",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2023-01-25 19:20:33"
},
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "member",
"confirmed": false,
"joined_at": "2023-01-25 19:23:31"
}
]
}
]
}
I want to return only array objects that match the user's id and "confirmed" value of "true" in the members array like this:
{
"sucess": true,
"message": "Users groups",
"data": [
{
"_id": "63987c4263d7eba1e79a1df1",
"created_at": "2022-12-13 13:21:03",
"group_name": "Demo 1",
"is_owner": true,
"balance": {
"$numberDecimal": "250.00"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd8",
"name": "Joy Kudosen",
"status": "member",
"confirmed": true,
"joined_at": "2022-12-14 13:22:03"
},
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
},
{
"_id": "63987c5a63d7eba1e79a1df4",
"created_at": "2022-12-13 13:21:27",
"group_name": "Demo 2",
"is_owner": true,
"balance": {
"$numberDecimal": "100"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-13 13:21:27"
}
]
},
{
"_id": "6399ae06ebaca8cd5fbcd639",
"created_at": "2022-12-14 11:05:39",
"group_name": "Demo 4",
"is_owner": true,
"balance": {
"$numberDecimal": "0"
},
"members": [
{
"user_id": "63987b7c63d7eba1e79a1dd7",
"name": "Joy Kudosen",
"status": "admin",
"confirmed": true,
"joined_at": "2022-12-14 11:05:39"
}
]
}
]
}
I have used different forms of this query but I am unable to filter out the "hello world" group information. I have tried aggregation and the "or" operation and still get the same results.
"$elemMatch" is used to find documents that match multiple fields within an array element.
For example, like this:
db.groups.find({
"members": {
"$elemMatch": {
"user_id": "63987b7c63d7eba1e79a1dd7",
"confirmed": true
}
}
})
Try it on mongoplayground.net.

MongoDb extract array

I have a mongodb which I want to extract some specific data.
Here is my json:
{
"jobs" : [
{
"id": 554523,
"code": "1256-554523",
"name": "Banco de Talentos",
"status": "published",
"type": "vacancy_type_effective",
"publicationType": "external",
"numVacancies": 1,
"departmentId": 108141,
"departmentName": "FUTURAS OPORTUNIDADES",
"roleId": 169970,
"roleName": "BANCO DE TALENTOS",
"createdAt": "2020-10-30T12:23:48.572Z",
"updatedAt": "2020-12-30T23:21:30.403Z",
"branchId": null,
"branchName": null
},
{
"id": 616834,
"code": "1256-616834",
"name": "YYYYYY (o) YYYYY",
"status": "frozen",
"type": "vacancy_type_effective",
"publicationType": "external",
"numVacancies": 1,
"departmentId": 109190,
"departmentName": "TESTE TESTE",
"roleId": 165712,
"roleName": "SL - TESTE PL",
"createdAt": "2020-12-16T14:17:36.187Z",
"updatedAt": "2021-01-29T17:08:43.613Z",
"branchId": 120448,
"branchName": "TESTE TESTE1"
}
],
"application": [
{
"id": 50707344,
"score": 40.251965332031254,
"partnerName": "indeed",
"endedAt": null,
"createdAt": "2020-12-21T11:21:30.587Z",
"updatedAt": "2021-02-18T22:02:35.866Z",
"tags": {},
"candidate": {
"birthdate": "1986-04-04",
"id": 578615,
"name": "TESTE",
"lastName": "TESTE TESTE",
"email": "teste#teste.com.br",
"identificationDocument": "34356792807",
"countryOfOrigin": "BR",
"linkedinProfileUrl": "teste",
"gender": "female",
"mobileNumber": "+5511972319799",
"phoneNumber": "(11)2463-2039"
},
"job": {
"id": 619713,
"name": "XXXXde XXXX Pleno"
},
"manualCandidate": null,
"currentStep": {
"id": 3527370,
"name": "Cadastro",
"status": "done"
}
},
{
"id": 50707915,
"score": 3.75547943115234E+1,
"partnerName": "indeed",
"endedAt": null,
"createdAt": "2020-12-21T11:31:31.877Z",
"updatedAt": "2021-02-18T14:07:06.605Z",
"tags": {},
"candidate": {
"birthdate": "1971-10-02",
"id": 919358,
"name": "TESTE TESTE",
"lastName": "SILVA",
"email": "teste.teste#teste.com",
"identificationDocument": "3232323232",
"countryOfOrigin": "BR",
"linkedinProfileUrl": "teste/",
"gender": "female",
"mobileNumber": "11 94021- 5521",
"phoneNumber": "+5511995685247"
},
"job": {
"id": 619713,
"name": "Analista de XXXXX Pleno"
},
"manualCandidate": null,
"currentStep": {
"id": 3527370,
"name": "Cadastro",
"status": "done"
}
}
]
}
My question is: How can I extract only the array objects in jobs and application separately? Anyone knows the code in Mongodb for do this?
I need do this task for after I can insert the extract separated data in different collections.
Thanks a lot.
Unfortunately there is no real "good" way of doing this. Here is an example of how I would do it by using $facet and other operators to manipulate the structure
db.collection.aggregate([
{
$match: {
/** your query*/
}
},
{
$facet: {
jobs: [
{
$unwind: "$jobs"
},
{
$replaceRoot: {
newRoot: "$jobs"
}
}
],
applications: [
{
$unwind: "$application"
},
{
$replaceRoot: {
newRoot: "$application"
}
}
]
}
},
{
$addFields: {
"merged": {
"$concatArrays": [
"$jobs",
"$applications"
]
}
}
},
{
$unwind: "$merged"
},
{
"$replaceRoot": {
"newRoot": "$merged"
}
}
])
Mongo Playground
I would personally just do it in code after you fetched the document.

Filter for one attribute (array) for one of its value (json)

Having the following record
{
"name": "
 Festões Plástico, 12mt x 17cm - Festas Populares",
"categories": [
"Festas",
"Casamentos",
"Decorações"
],
"hierarchicalCategories": {
"lvl0": "Festas",
"lvl1": "Festas > Casamentos",
"lvl2": "Festas > Casamentos > Decorações"
},
"description": "",
"brand": "Misterius",
"price": 14.94,
"stock": "Disponível",
"prices": [
{
"value": 12,
"type": "specificValue",
"family": "fatos",
"subfamily": "example"
},
{
"value": 13,
"type": "specificValue13",
"family": "fatos13",
"subfamily": "example13"
},
{
"value": 14,
"type": "specificValue14",
"family": "fatos14",
"subfamily": "example14"
},
{
"value": 15,
"type": "specificValue15",
"family": "fatos15",
"subfamily": "example15"
},
{
"value": 16,
"type": "specificValue16",
"family": "fatos16",
"subfamily": "example16"
}
],
"color": [
{
"name": "Amarelo",
"label": "Amarelo,#FFFF00",
"hexa": "#FFFF00"
},
{
"name": "Azul",
"label": "Azul,#0000FF",
"hexa": "#0000FF"
},
{
"name": "Branco",
"label": "Branco,#FFFFFF",
"hexa": "#FFFFFF"
},
{
"name": "Laranja",
"label": "Laranja,#FFA500",
"hexa": "#FFA500"
},
{
"name": "Verde Escuro",
"label": "Verde Escuro,#006400",
"hexa": "#006400"
},
{
"name": "Vermelho",
"label": "Vermelho,#FF0000",
"hexa": "#FF0000"
}
],
"specialcategorie": "",
"reference": "3546",
"rating": 0,
"free_shipping": false,
"popularity": 0,
"objectID": "30"
}
Now by searching for "Festas Populares" will return the record and its attributes, is it possible to also filter for one attribute array as "prices" to only return one json. for example "prices.type"="specificValue14" and "family"="fatos14" and "family"="fatos" and "subfamily"="example"
{
“value”: 14,
“type”: “specificValue14”,
“family”: “fatos14”,
“subfamily”: “example14”
}
the record return would be:
{
"name": "
 Festões Plástico, 12mt x 17cm - Festas Populares",
"categories": [
"Festas",
"Casamentos",
"Decorações"
],
"hierarchicalCategories": {
"lvl0": "Festas",
"lvl1": "Festas > Casamentos",
"lvl2": "Festas > Casamentos > Decorações"
},
"description": "",
"brand": "Misterius",
"price": 14.94,
"stock": "Disponível",
"prices": [
{
"value": 14,
"type": "specificValue14",
"family": "fatos14",
"subfamily": "example14"
}
],
"color": [
{
"name": "Amarelo",
"label": "Amarelo,#FFFF00",
"hexa": "#FFFF00"
},
{
"name": "Azul",
"label": "Azul,#0000FF",
"hexa": "#0000FF"
},
{
"name": "Branco",
"label": "Branco,#FFFFFF",
"hexa": "#FFFFFF"
},
{
"name": "Laranja",
"label": "Laranja,#FFA500",
"hexa": "#FFA500"
},
{
"name": "Verde Escuro",
"label": "Verde Escuro,#006400",
"hexa": "#006400"
},
{
"name": "Vermelho",
"label": "Vermelho,#FF0000",
"hexa": "#FF0000"
}
],
"specialcategorie": "",
"reference": "3546",
"rating": 0,
"free_shipping": false,
"popularity": 0,
"objectID": "30"
}
for some context a product can have multiple prices associated, for a specific user, or one day there is campaign giving discount, etc so for that cases want to filter price associated to the product/record.
No, this is not possible with Algolia. Records are always returned with the attributes specified inside attributesToRetrieve. These attributes are returned in full.

what is the date format from stash api?

In the below json response, what is the date format for createdDate and updatedDate? I am not sure how to work in reverse to find what format the api is using for date. I couldn't find this any where in the documentation.
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"id": 101,
"version": 1,
"title": "Talking Nerdy",
"description": "It’s a kludge, but put the tuple from the database in the cache.",
"state": "OPEN",
"open": true,
"closed": false,
"createdDate": 1359075920,
"updatedDate": 1359085920,
"fromRef": {
"id": "refs/heads/feature-ABC-123",
"repository": {
"slug": "my-repo",
"name": null,
"project": {
"key": "PRJ"
}
}
},
"toRef": {
"id": "refs/heads/master",
"repository": {
"slug": "my-repo",
"name": null,
"project": {
"key": "PRJ"
}
}
},
"locked": false,
"author": {
"user": {
"name": "tom",
"emailAddress": "tom#example.com",
"id": 115026,
"displayName": "Tom",
"active": true,
"slug": "tom",
"type": "NORMAL"
},
"role": "AUTHOR",
"approved": true
},
"reviewers": [
{
"user": {
"name": "jcitizen",
"emailAddress": "jane#example.com",
"id": 101,
"displayName": "Jane Citizen",
"active": true,
"slug": "jcitizen",
"type": "NORMAL"
},
"role": "REVIEWER",
"approved": true
}
],
"participants": [
{
"user": {
"name": "dick",
"emailAddress": "dick#example.com",
"id": 3083181,
"displayName": "Dick",
"active": true,
"slug": "dick",
"type": "NORMAL"
},
"role": "PARTICIPANT",
"approved": false
},
{
"user": {
"name": "harry",
"emailAddress": "harry#example.com",
"id": 99049120,
"displayName": "Harry",
"active": true,
"slug": "harry",
"type": "NORMAL"
},
"role": "PARTICIPANT",
"approved": true
}
],
"link": {
"url": "http://link/to/pullrequest",
"rel": "self"
},
"links": {
"self": [
{
"href": "http://link/to/pullrequest"
}
]
}
}
],
"start": 0
}
Just making a note that in my case, it is a UNIX timestamp, but I have to remove three trailing zeroes. E.g. the data looks like this:
"createdDate":1555621993000
If interpreted as a UNIX timestamp, that would be 09/12/51265 # 4:16am (UTC).
By removing the three trailing zeroes I get 1555621993, which is the correct time 04/18/2019 # 9:13pm (UTC)
Your mileage may vary but that was a key discovery for me :)
It looks like a UNIX timestamp.
https://en.wikipedia.org/wiki/Unix_time

Updating All Nested Arrays in Mongo Object

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++;
});