Mongodb group by on internal element - mongodb

I have data in following format
{
_id:ObjectId("someid"),
"masterKey":{
"key1":"val1",
"key2":"val2",
"key3":"val3",
"key4":"val1",
"key5":"val2",
"key6":"val3",
}
}
And I am expecting result which group the duplicate values of master key.
The result should be similar to this. Or in any other format which finds duplicate values of keys with key name.
{
_id:Object("someid"),
"masterKey":{
"val1":["key1","key4"],
"val2":["key2","key5"],
"val3":["key3","key6"]
}
}

Related

how to prevent in mongodb that keys are auto arranged in ascending in object type while insert/update

In my case i have schema as mentioned below. in this schema values key has an object with dynamic key:value pairs. while i am inserting/updating using this schema the sequence of my key value pair are changed to acceding. How can i prevent this.??
const mySchema = new Schema({
formName: String
values: Object
})
example...
my data to insert/update is... and i want to store it in same sequence.
{
formName: "my Form",
values: {
textBox1:"Value1",
dropdown1:"Value2",
textBox2:"Value3",
}
}
but the data is stored in below given sequence..
{
formName: "my Form",
values: {
dropdown1:"Value2",
textBox1:"Value1",
textBox2:"Value3",
}
}
You might need to use an array for that.
Instead of inserting/updating values via Object, it would be better to store key and value in array.
{
formName: "my Form",
keys: ["textBox1","dropdown1","textBox2"],
values: ["value1","value2","value3"]
}

ParseSwift ParseObject QueryConstraint

I have two collections in a mongo DB.
Here is how a document looks in the first collection (MainCollection):
_id
:"mzWqPEDYRU"
TITLE
:"ZAZ: I want."
ownerID
:"lGutCBY52g"
accessKey
:"0kAd4TOmoK0"
_created_at
:2020-03-13T11:42:11.169+00:00
_updated_at
:2020-03-13T17:08:15.090+00:00
downloadCount
:2
And here is how it looks in the second collection (SecondCollection):
_id
:"07BOGA8bHG"
_p_unit
:"MainCollection$mzWqPEDYRU"
SENTENCE
:"I love nature peace and freedom."
Order
:5
ownerID
:"lGutCBY52g"
AUDIO
:"07067b5589d1edd1d907e96c1daf6da1_VOICE.bin"
_created_at
:2020-03-13T11:42:17.483+00:00
_updated_at
:2020-03-13T11:42:19.336+00:00
There is a parent children relationship between the first and the second collection. In the last document we can see the _p_unit field where the "mzWqPEDYRU" part points to the id of the parent in the first collection.
I have one problem from start with the following code:
func theFunction() {
do {MainCollection.query().find() {
result in
switch result {
case .success(let items):
print("items.count = \(items.count)")
for item in items {
/// ....
}
case .failure(let error):
print("Error in \(#function): \(error)")
}
}
}
}
The way this above code is written works fine and I get the number of elements in MainCollection as one would expect. But then comes a less expected behaviour, in this same code if I replace MainCollection by SecondCollection, instead of getting the number of elements in SecondCollection as I would think. I get an error like:
ParseError(code: ParseSwift.ParseError.Code.unknownError,
message: "Error decoding parse-server response:
Optional(<NSHTTPURLResponse: 0x2837211a0> { URL:}
{ Status Code: 200, Headers {} }) with error:
The data couldn’t be read because it isn’t in the correct format.
Format: Optional(\"{\\\"results\\\": .......
Can anybody point out what is causing this?
It is something like:
var SecondCollection.query(unit == documentOne).find()
The .query() method works in a key/value scheme so it should pass the key as a string and the value as the referenced type, so passing "unit" between double quotes is correct:
do {SecondCollection.query("unit" == cell).find() {
The error you're getting is because cell is a Parse.Object and it is expecting a value in that place (a property in this case).
Please try the following and see if it works for you:
do {SecondCollection.query("unit" == cell.id).find() {

Is it possible to filter model's subfield in Strapi's GraphQL?

With strapi GraphQL it is possible to limit the number of returned entries. However, is it also possible to limit the number of returned subentries? For example, if I have "galleries" content type with a field called "images", can I limit the number of Images returned?
This is what I tried and works:
query mainQuery {
galleries(limit: 1) {
name
image {
url
id
}
}
}
And this is what I would actually like to do but does not work. There is no error or anything but the query returns all results, not just 1:
query mainQuery {
galleries {
name
image(limit: 1) {
url
id
}
}
}
I would like to fetch all galleries but only one image in each of them. Can anybody help me solve this?

jq to fetch value using key from a nested json when a key can be deeply nested

I have a nested JSON as shown below. I need to list down the values of all '.url' no matter how deeply it is nested.
{
section1: {
url: "abc/efg/dgh.com",
name: "test1"
},
section2: {
.section3: {
url: "efef/dedede/efdgh.com",
name: "test2"
}
}
}
The expected output is as follows:
["abc/efg/dgh.com", "efef/dedede/efdgh.com"]
Thanks in advance
If you need to access something "no matter how deeply it is nested", then recurse is your friend.
jq '[recurse|.url? // empty]' file.json
The question mark prevents us from failing when the recursion reaches something that is not an object (eg. the leaf strings). And //empty replaces the nulls we get from objects without an "url" key with empty results (so it deletes them).

Calculated Field - Syntax Error

I am running a mail merge off a data set where I have fields "pledge" and "distance"
{ pledge } Yields a correct value when the merge is previewed.
Yet...
{ ={ pledge}*{distance} } Results in a syntax error.
How can I get this field to calculate correctly?
{ =pledge * distance} fixed it