Mean stack & PrimeNG CSV export - nested array of objects as new row - export-to-csv

I have a multiple level nested array that I would like to export. I'm just wondering if I can export the nested array of objects as a new row of csv? At the moment, I'll have to input "users.0.name" as field in order to get the data of the array of objects instead of [Object Object],[Object Object],[Object Object]. I tried to do a loop on the this.cols but it doesn't work. I'm thinking to create another record of the same data but with just one user so that I can export one user for each row of data. Is this possible? Thank you in advance!
example of data:
[
users: [
{
id: 9845397834,
name: "John",
title: {...}
},
{
id: 65156169834,
name: "Mary",
title: {...}
},
{
id: 8965134,
name: "Jane",
title: {...}
},
],
phone: "",
email: ""
]

Related

Prisma - adding new object into existing array and sorting it afterwards

I'm using Prisma in combination with MongoDB and this is how I currently update existing document:
await prisma.H2H.update({
where: { id: existingH2HDocument.id },
data: {
matches: {
push: {
matchId: editInput.matchId,
leagueId: editInput.leagueId,
date: editInput.date,
season: editInput.season,
},
},
},
})
Basically my model of the document looks like this:
id: 1234
matches: [
{
object1
},
{
object2
}
...
]
I'm adding new objects into the matches array, is there a way how to sort it during the update?
Using push adds them to the bottom but I need the newly added objects on the top(so basically an unshift).

How to search a Mongo collection for a document that contains an array that contains an element that meets a particular criteria?

I'm new at Mongo and there might be a better way to do what I want. I'm dealing with a particular data structure that my application must process.
Suppose that I have a collection that contains two documents that contain information about universities and their student clubs to include the name of each club and the name of each student in each club along with their age:
{
_id: 1, // object ID
name: "Oxford University",
clubs: [{
name: "Soccer",
members: [{
name: "Alice",
age: 22
},
{
name: "Bob",
age: 23
}
]
}, {
name: "Gymnastics",
members: [{
name: "Charlie",
age: 20
},
{
name: "Dorothy",
age: 19
}
]
}]
}
{
_id: 2, // object ID
name: "Cambridge University",
clubs: [{
name: "Chess",
members: [{
name: "Ellen",
age: 30
},
{
name: "Frank",
age: 35
}
]
}, {
name: "Go",
members: [{
name: "Gilbert",
age: 25
},
{
name: "Hikari",
age: 40
}
]
}]
}
Suppose that I want to write a query on this collection that will find universities that have a club that has at least one member aged 40 or older.
How do I do that?
I sketched this example based off of the idea of taking some JSON documents and inserting them into a new collection. Maybe it would be a better idea to break this apart into multiple collections. I just had the idea to research if Mongo might be a good product to use in this situation given that a big part of my job here is to create something that can receive some JSON data, process it, and make it queryable.
MongoDB queries have a convenient feature to query documents that have a specific value or condition in embedded objects and arrays. In the query you can specify the "path" to the object or array using "dot notation" without having to specify the exact array index, etc.
Using your example, you can find the documents where a member of a club is aged 40 or older like this.
db.collection.find({
"clubs.members.age": {
"$gte": 40
}
})
This returns the second document in your example collection.
Try it on mongoplayground.net.

How to migrate Legacy Realm JSON data to MongoDB Realm (Atlas) - problem with relationships

I'm trying to migrate from Legacy Realm to MongoDB Realm and I'm having trouble migrating data with relationships.
Example schema:
{
name: 'Location',
primaryKey: '_id',
properties: {
_id: 'string',
_partition: 'string?',
Name: 'string',
WorkTime: 'WorkTime[]',
},
}
{
name: 'WorkTime',
primaryKey: '_id',
properties: {
_id: 'string',
_partition: 'string?',
Day: 'string?',
Open: 'string?',
Closed: 'string?',
},
}
In what format should I push the json to MongoDB Atlas, so it links with the related data?
I tried adding the Ids (with and without partition string) from my WorkTime collection, following https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/
Example JSON:
{
"_id":"adaaade7-2b4f-5342-92a1-f00857a61253"
"_partition":"TestPartition",
"Name":"Test",
"WorkTime":[
{
"_partition":"TestPartition",
"_id":"1229ac83-f390-00e3-14a7-ffa9f8eb36e4"
},
{
"_partition":"TestPartition",
"_id":"9af60ff2-064b-3f09-6621-e7f949b003be"
},
{
"_partition":"TestPartition",
"_id":"04c33a8c-5e59-45ab-fbf8-e70446324475"
},
],
}
And when I try to fetch the data from realm (realm.objects('Location')), I don't get anything. Locations show up only when WorkTime is empty.
I also tried passing the whole WorkTime object, instead of Id, but then it creates a copy of the full object.
I figure it out, the referenced objects need to be Id strings, like in the example. https://docs.mongodb.com/manual/tutorial/model-referenced-one-to-many-relationships-between-documents/
So, if you have
{
"_id":"adaaade7-2b4f-5342-92a1-f00857a61253"
"_partition":"TestPartition",
"Test": {"_id":"9af60ff2-064b-3f09-6621-e7f949b003be", "_partition":"TestPartition"},
"WorkTime":[
{
"_partition":"TestPartition",
"_id":"1229ac83-f390-00e3-14a7-ffa9f8eb36e4"
},
],
}
should be converted to
{
"_id":"adaaade7-2b4f-5342-92a1-f00857a61253"
"_partition":"TestPartition",
"Test": "9af60ff2-064b-3f09-6621-e7f949b003be",
"WorkTime":[
"1229ac83-f390-00e3-14a7-ffa9f8eb36e4"
],
}
Leaving only the Ids as strings.
I iterated though the objects recursively, deleted non-id props of nested objects, replaced Id with _id and added partition key. After that you have to replace the nested objects with string {"_id":"9af60ff2-064b-3f09-6621-e7f949b003be"} -> 9af60ff2-064b-3f09-6621-e7f949b003be.

fetching documents based on nested subdoc array value

I'm trying to get all documents in a collection based on a subdocument array values. This is my data structure in the collection i'm seeking:
{
_id: ObjectId('...'),
name: "my event",
members:
[
{
_id: ObjectId('...'),
name: "family",
users: [ObjectId('...'),ObjectId('...'),ObjectId('...')]
},
{
_id: ObjectId('...'),
name: "work",
users: [ObjectId('...'),ObjectId('...'),ObjectId('...')]
}
]
}
I should point out that the schema of these objects are defined like so:
Events:
{
name: { type: String },
members: {type: [{ type: ObjectId, ref: 'MemberGroup' }], default:[] }
}
MemberGroup:
{
name: { type: String },
users: [{type: mongoose.Schema.Types.ObjectId, ref: 'User'}]
}
and of course User is just an arbitrary object with an id.
What i'm trying to fetch: i want to retrieve all events which has a specific user id in its member.users field.
i'm not sure if its event possible in a single call but here is what i've tried:
var userId = new mongoose.Schema.ObjectId('57d9503ef10d5ffc08d6b8cc');
events.find({members: { $elemMatch : { users: { $in: [userId]} } }})
this syntax work but return no elements even though i know there are matching elements (using robomongo to visualize the db)
So after a long search in stackoverflow i came across the good answare here:
MongoDB: How to find out if an array field contains an element?
my query changed to the following which gave me my desired result:
var userId = new mongoose.Schema.ObjectId('57d9503ef10d5ffc08d6b8cc');
events.find({}, {members: { $elemMatch : { users: { $eq: userId} } }})
notice the use of the second find parameter to specify the query limit instead of the first one (which is odd)

Mongodb: How to add unique value to each element in array?

I'm a new user of mongodb and I have a model like below. For update list data, I have to specify the element in an array. So I think I need to store a unique value for each element. Because list.name and list.price are variable data.
So are there any good ways to create an unique id in mongodb? Or should I create unique ids by myself?
{
name: 'AAA',
list: [
{name: 'HOGE', price: 10, id: 'XXXXXXXXXX'}, // way to add id
{name: 'FUGA', price: 12, id: 'YYYYYYYYYY'} // way to add id
]
}
Mongodb creates unique id only for documents. There is no better way for list or array elements. So, you should create Unique ids yourself.
Add keep in mind that, While updating your list use $addToSet.
For more information of $addToSet follow this documentation
use ObjectId() on your id field, so like..
db.test.update({name: "AAA"}, { $push: { list: {_id : ObjectId(), name: "dingles", price: 21} }});
reference: https://docs.mongodb.org/v3.0/reference/object-id/
whoever is seeing this in 2022, mongodb creates unique ids automatically we just have to provide schema for that particular array.
like,
_id : {
type: String
},
list: {
type: [{
Name : {
type: String
},
price : {
type: String
}
}]
}
this schema will generate auto id for all elements added into array
but below example will not create it.
_id : {
type: String
},
list: {
type: Array
}