MongoDB - How to update if and only if all ids are matching inside a list's object - mongodb

Lets say I have an Object in MongoDB that looks like this:
"obj":
{
"list":
[
{ "_id": "123" },
{ "_id": "456" },
],
"result": 42
}
Lets say I have an Event Object (happens to be in Kotlin, but do not mind the programming language):
class Event(val ids: Set<String>, val newResult: Int)
How to construct an update query in MongoDB to set the result if and only if the ids in the Event Object match the ids inside the list.

Related

use JsLookup for multiple level array using play Json for lists with objects and simple lists

i have a function that exctract from a json list of elements based on the path i give it.
the func looks like this:
def findAllValuesAtPath(jsValue: JsObject, path: String): List[JsValue] = {
val jsPath = JsPath(path
.split("\\[\\*]\\.")
.flatMap(s => s.split("\\.")
.map(RecursiveSearch)
).toList)
jsPath(jsValue)
}
for example:
{
"person": {
"kids": [
{
"name": "josh",
"age": 5
},
{
"name": "julia",
"age": 13
}
]
}
}
now if i give the path - "person.kids[*].name" I will get list of their names List("josh", "julia") which its what i want.
but if the list of kids were just simple list like:
{
"person": {
"kids": [
"josh",
"julia"
]
}
}
and i will give the path - "person.kids[*]" I will get empty list List() and I want to get this as List("josh", "julia") (without the brakets)
do you see any way to improve my func to handle both cases?

Pymongo access specific field value within nested dict

In Pymongo application, while iterating through every document of the collection, how to access a specific field value of the JSON structure?
{
"_id": {
"$oid": "5e1c2b0bacbdaehujjjbdsh"
},
"a": {
"data_type": "abc",
"data_format": "xyz",
"data_version": "1",
},
"b": "123",
"c": "345"
}
Based on the following code snippet, how do I access the value associated with the key 'data_format' which is nested within the key 'a' ---
for document in col.find():
data_format_val = document['a']['data_format'] # not working
Relatively new to Mongodb query commands.
It's possible that some of the documents of the collection may not have the key 'a'.
Try using $exists to make sure the field is present like this: Syntax: { field: { $exists: } }

MongoDB Partial Unique Index on Object Field in Array Field

Lets say I have a collection whose objects have a structure like this:
{
"a": [
{"name": "foo", "meh": "whatever"},
{"name": "bar", "meh": "hem"}
],
"other_stuff": { }
}
It's possible for an object in this collection to not have the "a" field. I'd like to enforce a constraint on the database such that, if an object has the "a" field, that none of the "name" fields in objects contained in that "a" field are duplicates across the entire collection.
So for example, the following object would be flagged as a constraint violation if the above object were already in the collection:
{
"a": [
{"name": "bar", "meh": "meh meh"}
],
"other_stuff": { }
}
Furthermore, the following object would be flagged as a constraint violation regardless of other documents already in the collection:
{
"a": [
{"name": "boo", "meh": "blub"},
{"name": "boo", "meh": "glub"}
],
"other_stuff": { }
}
Is it possible to specify a partial unique index for MongoDB? If it's different between Mongo 3.2, 3.4, 3.6 and 4.0 it would be nice to know that too - I don't care about earlier than 3.2.
I was thinking it might be something like this to prevent duplication across documents (because in Javscript [] > '' === false and undefined > '' === false and ['anything'] > '' === true for the passive reader):
db.MyCollection.createIndex(
{ "a.name": 1 },
{
"unique": true,
"partialFilterExpression": {
"a": { "$gt": '' }
}
}
)
... and I think the only way to prevent duplicate values within a document is to enforce in application logic the use of the $addToSet operator when operating on the "a" field.
I'd be happy to be corrected or corroborated on either count.

mongodb: return an array of document ids

Is it possible to query mongodb to return array of matching document id values, without the related keys?
Please consider following 'parent' data structur:
{
"_id": ObjectId("52448e4697fb2b775cb5c3a7"),
"name": "Peter",
"children": [
{
"name": "joe"
}
]
},
{
"_id": ObjectId("52448e4697fb2b775cb5c3b6"),
"name": "Marry",
"children": [
{
"name": "joe"
}
]
}
I would to query for an array of parent _ids whose children have the name "joe"
For provided sample data, I would like the following output returned from mongo:
[ObjectId("52448e4697fb2b775cb5c3a7"), ObjectId("52448e4697fb2b775cb5c3b6")]
I know that I can query for an output like this, which also contains the keys
[{"_id": ObjectId("52448e4697fb2b775cb5c3a7")}, {"_id": ObjectId("52448e4697fb2b775cb5c3b6")}]
However I need to push above array to another document with an update operation like this:
db.statistic.update({"date": today}, {$push: {"children": [ObjectId("52448e4697fb2b775cb5c3a7"), ObjectId("52448e4697fb2b775cb5c3b6")]}}, true, false)
I would like to avoid sorting out the document structure, in case it is possible to just return an array containing the appropriate values using mongo
It should be possible by
db.coll.distinct("_id", {"children.name": "joe"})

Duplicating a Mongoose document with subdocs, duplicated id of subdocs are allowed?

To clarify: I have a document with a subdoc. I create a new document with the same data of the other one and it gets a new id. However, when I copy the subdoc array they do not get a new id.
Are subdocs id local to the parent doc? I.e. would the following be a problem?
[
{
"__v": 1,
"_id": "5214af03a9f53efa61000004",
"name": "Foo",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
},
{
"__v": 0,
"name": "Foo",
"_id": "5214af03a9f53efa61000014",
"subdocs": [
{
"thing": "value",
"_id": "5214af03a9f53efa61000006"
}
]
}
]
There is a unique index on the _id field of documents stored directly in a collection, but not for embedded documents, nor is there any requirement that embedded documents have an _id field at all. The two documents you have provided are both valid to be stored in MongoDB in the same database (I'm interpreting your example as an array of two documents that are both stored directly in a collection together).