I know I can use the pipeline aggregator within update to update the field of one value with the value from another field. However, my issue is when updated a field value based on the value of a nested field. The result of the update always issues the new field with brackets. I don't want brackets/array, I just want it to be a value. See code below
https://mongoplayground.net/p/7ZDP8CYtKK3
db={
"players": [
{
"_id": ObjectId("5fba17c1c4566e57fafdcd7e"),
"username": "moshe",
"health": 0,
"maxHealth": 200,
"Chapters": [
{"Cat A": 25,
"Cat B": 100,
"Cat C": 125}]
}
]
}
Here's the query I apply below
db.players.update(
{username: "moshe"},
[{"$set": {"health": "$Chapters.Cat A"}}]
)
The result yields
[{"Chapters": [
{"Cat A": 25,
"Cat B": 100,
"Cat C": 125}],
"_id": ObjectId("5fba17c1c4566e57fafdcd7e"),
"health": [25],
"maxHealth": 200,
"username": "moshe"
}]
What I want is for the update to health to appear without array brackets as so.... "health":25
Again this is an example based on a much much larger DB I'm working with.
You can use $arrayElemAt or $first(v4.4) operators to select the first element from an array,
db.players.update(
{ username: "moshe" },
[{
"$set": {
"health": {
"$arrayElemAt": ["$Chapters.Cat A", 0]
}
}
}]
)
Playground
Related
I want to use an aggregation to get the highest value of a specified key that's in a dict field that's in a list field that's in a document that's in a mongodb collection.
Here's some example data
[
{
"name": "hi",
"hist": [
{
"username": "bill",
},
{
"username": "jack",
"changed_from": 127
}
]
},
{
"name": "member1",
"hist": [
{
"username": "asdf",
"changed_from": 123
},
{
"username": "duhby",
"changed_from": 126
}
]
},
{
"name": "member5",
"hist": [
{
"username": "duhby",
"changed_from": 150
},
{
"username": "test",
"changed_from": 123
},
{
"username": "duhby",
"changed_from": 125
}
]
}
]
I want to be able to put in duhby as the username, for example, and get at least a list of results I can then easily get the maximum value of, with the maximum value in this case being 150.
I tried using an aggregate group but got stuck when trying to only get the data from the specific username, and not just all documents that had that username in the hist field.
db.collection.aggregate([
{
$group: {
"_id": "$hist.duhby",
update_time: {
$max: "$hist.changed_from"
}
}
}
])
With the example data shown earlier, this returns:
[
{
"_id": [],
"update_time": [
150,
123,
125
]
}
]
However, this isn't useful because it shows every changed_from value when I want it to only show (and sort by) the ones with the username specified. Expected result:
[
{
"_id": [],
"update_time": [
150,
125
]
}
]
I also want to be able to get the original document and maybe have the name field as the id in the aggregation, but the id is currently returned as an empty list.
I realized here that what I needed is just a find, not an aggregation, because I was just trying to see if any document existed that had the embedded document with a key value pair of a certain value. The following does what I was looking for:
db.collection.find({
"hist": {
$elemMatch: {
username: "duhby",
changed_from: {
"$lte": 123
}
}
}
})
From the contacts, I'd like to select the values in fields: "Id" (47) and everything from the nested array [doNotContact]. I could use some help defining the JSONPath-filter I should be using to select the values: 47 and each value inside the nested array.
{
"total": "1",
"contacts": {
"47": {
"id": 47,
"isPublished": true,
"dateAdded": "2015-07-21T12:27:12-05:00",
"createdBy": 1,
"createdByUser": "Joe Smith",
"doNotContact": [{
"id": 2,
"reason": 2,
"comments": "",
"channel": "email",
"channelId": null
}]
}
}
}
I have tried paths like: $.contacts.*.['id','doNotContact'] however, this does not seem to work. I am using the website: https://goessner.net/articles/JsonPath/ normally this would help me solve the problem.
Not all implementations support the comma-delimited selectors, e.g. ['id','doNotContact']. See the JSON Path comparison project site (specifically this test) for information as to which implementations support the syntax.
Secondly, please see this answer about omitting the dot before a bracket syntax
I am trying to project two elements of an array I got after joining two documents using $lookup. When I use the dot notation to access the array elements as below -
db.departments.aggregate([{
$lookup: {
from: 'employees',
localField: 'dep_id',
foreignField: 'department',
as: 'emps'
}
}, {
$project: {
_id: 0,
emp_id: 1,
salary: 1,
emp_name: '$emps.name',
username: '$emps.username'
}
}])
I get the following result -
emp_id:910579
salary:100000
emp_name:Array
0:"Stephen Wolf"
username:Array
0:"StepWolf"
I want the result as follows -
emp_id:910579
salary:100000
emp_name:"Stephen Wolf"
username:"StepWolf"
Does anybody have any suggestions? I want something that can convert array element to attribute, similar to the ObjectToArray function.
Query1
the bellow does field : [m1 m2 m2] => field : m1
takes the first member and makes it the value of the field
Test code here
aggregate(
[{"$set":
{"emp_name": {"$arrayElemAt": ["$emp_name", 0]},
"username": {"$arrayElemAt": ["$username", 0]}}}])
Query2
If you have MongoDB >=5 you can use $first also
Test code here
aggregate(
[{"$set":
{"emp_name": {"$first": "$emp_name"},
"username": {"$first": "$username"}}}])
In your case use query1 or query2 inside the project you already have, like :
{
$project: {
_id: 0,
emp_id: 1,
salary: 1,
"emp_name": {"$first": "$emp_name"},
"username": {"$first": "$username"}
}
}
I am having trouble with querying a MongoDB collection with an array inside.
Here is the structure of my collection that I am querying. This is one record:
{
"_id": "abc123def4567890",
"profile_id": "abc123def4567890",
"image_count": 2,
"images": [
{
"image_id": "ABC123456789",
"image_url": "images/something.jpg",
"geo_loc": "-0.1234,11.234567890",
"title": "A Title",
"shot_time": "01:23:33",
"shot_date": "11/22/2222",
"shot_type": "scenery",
"conditions": "cloudy",
"iso": 16,
"f": 2.4,
"ss": "1/545",
"focal": 6.0,
"equipment": "",
"instructions": "",
"upload_date": 1234567890,
"update_date": 1234567890
},
{
"image_id": "ABC123456789",
"image_url": "images/something.jpg",
"geo_loc": "-0.1234,11.234567890",
"title": "A Title",
"shot_time": "01:23:33",
"shot_date": "11/22/2222",
"shot_type": "portrait",
"conditions": "cloudy",
"iso": "16",
"f": "2.4",
"ss": "1/545",
"focal": "6.0",
"equipment": "",
"instructions": "",
"upload_date": 1234567890,
"update_date": 1234567890
}
]
}
Forgive the formatting, I didn't know how else to show this.
As you can see, it's a profile with a series of images within an array called 'images' and there are 2 images. Each of the 'images' array items contain an object of attributes for the image (url, title, type, etc).
All I want to do is to return the object element whose attributes match certain criteria:
Select object from images which has shot_type = "scenery"
I tried to make it as simple as possible so i started with:
find( { "images.shot_type": "scenery" } )
This returns the entire record and both the images within. So I tried projection but I could not isolate the single object within the array (in this case object at position 0) and return it.
I think the answer lies with projection but I am unsure.
I have gone through the MongoDB documents for hours now and can't find inspiration. I have read about $elemMatch, $, and the other array operators, nothing seems to allow you to single out an array item based on data within. I have been through this page too https://docs.mongodb.com/manual/tutorial/query-arrays/ Still can't work it out.
Can anyone provide help?
Have I made an error by using '$push' to populate my images field (making it an array) instead of using '$set' which would have made it into an embedded document? Would this have made a difference?
Using aggregation:
db.collection.aggregate({
$project: {
_id: 0,
"result": {
$filter: {
input: "$images",
as: "img",
cond: {
$eq: [
"$$img.shot_type",
"scenery"
]
}
}
}
}
})
Playground
You can use $elemMatch in this way (simplified query):
db.collection.find({
"profile_id": "1",
},
{
"images": {
"$elemMatch": {
"shot_type": 1
}
}
})
You can use two objects into find query. The first will filter all document and will only get those whose profile_id is 1. You can omit this stage and use only { } if you wnat to search into the entire collection.
Then, the other object uses $elemMatch to get only the element whose shot_type is 1.
Check an example here
I would like to upsert an element in an array, based on doc _id and element _id. Currently it works only if the element is allready in the array (update works, insert not).
So, these collection:
[{
"_id": "5a65fcf363e2a32531ed9f9b",
"ressources": [
{
"_id": "5a65fd0363e2a32531ed9f9c"
}
]
}]
Receiving this request:
query = { _id: '5a65fcf363e2a32531ed9f9b', 'ressources._id': '5a65fd0363e2a32531ed9f9c' };
update = { '$set': { 'ressources.$': { '_id': '5a65fd0363e2a32531ed9f9c', qt: '153', unit: 'kg' } } };
options = {upsert:true};
collection.update(query,update,options);
Will give this ok result:
[{
"_id": "5a65fcf363e2a32531ed9f9b",
"ressources": [
{
"_id": "5a65fd0363e2a32531ed9f9c",
"qt": 153,
"unit": "kg"
}
]
}]
How to make the same request work with these initial collections:
[{
"_id": "5a65fcf363e2a32531ed9f9b"
}]
OR
[{
"_id": "5a65fcf363e2a32531ed9f9b",
"ressources": []
}]
How to make the upsert work?
Does upsert works with entire document only?
Currently, I face this error:
The positional operator did not find the match needed from the query.
Thanks
I also tried to figure out how to do it. I found only one way:
fetch model by id
update array manually (via javascript)
save the model
Sad to know that in 2018 you still have to do the stuff like it.
UPDATE:
This will update particular element in viewData array
db.collection.update({
"_id": args._id,
"viewData._id": widgetId
},
{
$set: {
"viewData.$.widgetData": widgetDoc.widgetData
}
})
$push command will add new items