how to merge similar values in normalizr function? - reactjs

I have unusual response from server like this
[
{
id: 1,
name: "Alexandr",
children: [
{
id: 2,
name: "Stephan"
},
{
id: 3,
name: "Nick"
}
]
},
{
id: 4,
name: "David",
children: [
{
id: 3,
name: "Nick"
},
{
id: 6,
name: "Paul"
}
]
}
]
i would like to normalize this response to receive a diction with all people. So, i use normalizr go flat this
const people= new Schema('people');
people.define({
Children: arrayOf(people),
NotOwnChildren: arrayOf(people)
});
let normalized = normalize(response.data, arrayOf(people));
but doing like this i get an error
"When merging two people, found unequal data in their "Children" values. Using the earlier value."
How can i adjust normalizr to merge people with same id (update entities with newest data)?

It looks like you're getting two people that have differing values for one of their keys (I'm assuming your example input is truncated).
For Normalizr#2:
You can use a custom mergeIntoEntity function to resolve the issue manually.
For Normalizr#>=3.0.0, you'll need use mergeStrategy.

Related

MongoDB transform array of objects into array of field values

I got a field which is an Array of objects inside another array of object in below format
{
id: '1234567',
listOfSettings: [{
name: 'setting1',
listOfObjects: [{name: 'object1',id:'111'}, {name: 'object2', id: '112'}]
}, {
name: 'setting2',
listOfObjects: [{name: 'object2',id:'113'}, {name: 'object3', id: '114'}]
}]
}
I want to convert it with a Mongo update query into something like:
{
id: '1234567',
listOfSettings:[{
name: 'settings1',
listOfObjects:['111', '112']
}, {
name: 'settings2',
listOfObjects:['113', '114']
}]
}
I know I can use the $set option to update the content and $fieldname to specify the field to obtain values, but not sure how I can achieve it in case of nested arrays.
Agg pipeline:
[{"$project"=>
{"listOfSettings"=>
{"$map"=>
{"input"=>"$listOfSettings",
"as"=>"x",
"in"=>
{"$mergeObjects"=>
["$$x",
{"listOfObjects"=>
{"$map"=>
{"input"=>"$$x.listOfObjects",
"as"=>"y",
"in"=>"$$y.id"}}}]}}}}}]
[{"_id"=>BSON::ObjectId('60aee7b82c97a61504d9b01a'),
"listOfSettings"=>
[{"name"=>"setting1", "listOfObjects"=>["111", "112"]},
{"name"=>"setting2", "listOfObjects"=>["113", "114"]}]}]

NextJs / React: Organizing Array

I'm having issues understanding how to best manipulate an array to get the data I want. From the research I've done, there's multiple ways, but I'm unclear on which is most optimized.
I want to display a simple list, with the items broken down by country, then state, then organized alphabetically by city. The array is formatted as follows:
[
{
id: 1,
name: "Place 1",
state: "Florida",
city: "Boca Raton",
country: "US",
},
{
id: 2,
name: "Place 2",
state: "Florida",
city: "Daytona Beach",
country: "US",
},
{
id: 3,
name: "Place 3",
state: "Kansas",
city: "Lenexa",
country: "US",
},
{
id: 4,
name: "Place 4",
state: "Harju",
city: "Tallinn",
country: "EE",
},
]
An example of the desired outcome is:
US
Florida
Place 1
Place 2
Kansas
Place 3
EE
Harju
Place 4
I see a lot of people saying to utilize ES6 for this, but I'm not sure the best way to approach it. Manipulate the original array response? Is there some way I can loop through them?
Here's an approach that only requires a single loop.
const data = [];
let result = {};
data.forEach(({ name, state, country }) => {
if (!result[country]) {
result[country] = {};
}
if (!result[country][state]) {
result[country][state] = [name];
}
else {
result[country] = {
...result[country],
[state]: [
...result[country][state],
name
]
};
}
});
console.log(result);
Output
{
US: { Florida: [ 'Place 1', 'Place 2' ], Kansas: [ 'Place 3' ] },
EE: { Harju: [ 'Place 4' ] }
}
I'm sure the if-else part can be removed by using spread operator and operator chaining, but I wasn't able to figure that out.
If your environment supports operator chaining, here's a smaller solution
const data = [];
let result = {};
data.forEach(({ name, state, country }) => {
result[country] = {
...result[country],
[state]: [
...(result?.[country]?.[state] || []),
name
]
};
});
console.log(result);

MongoDB check if key value exists and update only some fields

Let's say I have a MongoDB collection "people" that has the form
[
{
_id: [OBJECT_ID_1],
name: "Paul",
hobby: "rowing",
fixed: 1
},
{
_id: [OBJECT_ID_2],
name: "Selena",
hobby: "drawing",
fixed: 2
},
{
_id: [OBJECT_ID_3],
name: "Emily",
hobby: "jogging",
fixed: 3
}
]
And new data to be inserted of the form
var data = [
{
name: "Paul", // name exists, so keep "fixed" at 1
hobby: "archery",
fixed: 4
},
{
name: "Peter",
hobby: "knitting",
fixed: 5
}
]
I would like to insert/update the collection with the new data. However, if a document with the same "name" already exists, I do not want to update "fixed". The result after inserting the above data should be
[
{
_id: [OBJECT_ID_1],
name: "Paul",
hobby: "archery", // updated
fixed: 1 // not updated, because name existed
},
{
_id: [OBJECT_ID_2],
name: "Selena",
hobby: "drawing",
fixed: 2
},
{
_id: [OBJECT_ID_3],
name: "Emily",
hobby: "jogging",
fixed: 3
},
{ // newly inserted document
_id: [OBJECT_ID_4],
name: "Peter",
hobby: "knitting",
fixed: 5
}
]
The data includes a large number of documents, so I would like to achieve this in one query if possible. What would be the best way to accomplish this? Many thanks!
bulkWrite with updateOne's with $upsert:true seems to work best for you...
bulkWrite not perform a Find operation.
Its (in my case) is necessary to control if it will create a another document.
In my case I just use find check before insert/create method.
if (collection.find({"descr":descr}).limit(1).length === 1) {
//...create method
console.log('Exists')
}

MongoDB Compass GROUP BY value

Lets say that i have the following objects in my mongodb-compass-database:
{
_id: ObjectID("randomString"),
Name: "Test1",
OtherAttribute: 187
},
{
_id: ObjectID("otherRandomString"),
Name: "Test2",
OtherAttribute: 1337
},
{
_id: ObjectID("otherRandomString2"),
Name: "Test1",
OtherAttribute: 23
}
How can I return the "Name"-value if it exist more than one time?
In the example I want to receive "Test1" or the whole object, doesnt matter.
I just need to see if there are any duplicates.
I need to use the MongoDB Compass Find-Input:
Is this possible?

Ignoring a field mongodb request

Let's say we have objects like this in a mongodb collection:
{
_id: 00000001
colors: ["green", "yellow"],
houses: [
{
number: 1,
owner: "John"
},
{
number: 2,
owner: "John"
},
{
number:3,
owner: "Dave"
}
]
},
{
_id: 00000002
colors: ["green", "red"],
houses: [
{
number: 15,
owner: "Dave"
},
]
}
So, to get every object where the color array contains the color green the query I would need to write would look smth like this: collection.find({colors: "green"});
Now if I would like to get all the objects in which John owns a house, how would I formulate such a query?
Basically what I am asking is, if my query would be collection.find({houses: {owner: "John", number: ?}}) what would I need to replace the "?" with to tell mongo that I don't care what value number has.
Or maybe there is another approach that I haven't thought of?
Thank you for any help!
(Btw this is a made up example hence why the IDs look weird and the object in itself doesn't seem very useful.)
To query an array of objects you can use the dot notation, try:
db.collection.find({ "houses.owner": "John"}})

Resources