i have an array of information containing folders like this:
[
{
"ACLID" : 0,
"ID" : 100,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "04/12/2012 17:38:46",
"isSubFolder" : 0,
"name" : "Piyush1",
"objectType" : 3,
-->"parentID" : 3,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "04/12/2012 17:38:46"
},
{
"ACLID" : 0,
"ID" : 102,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/10/2012 12:38:25",
"isSubFolder" : 0,
"name" : "New",
"objectType" : 3,
-->"parentID" : 3,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/10/2012 12:38:25"
},
{
"ACLID" : 0,
"ID" : 103,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/14/2012 18:00:22",
"isSubFolder" : 0,
"name" : "SegFolderWithDiffPolicy",
"objectType" : 3,
-->"parentID" : 3,
"policyID" : 39,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/14/2012 18:00:22"
},
{
"ACLID" : 0,
"ID" : 104,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/17/2012 14:13:56",
"isSubFolder" : 0,
"name" : "New1",
"objectType" : 3,
-->"parentID" : 100,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/17/2012 14:13:56"
},
{
"ACLID" : 0,
"ID" : 105,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/17/2012 14:14:13",
"isSubFolder" : 0,
"name" : "abcasdna",
"objectType" : 3,
-->"parentID" : 104,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/17/2012 14:14:13"
},
{
"ACLID" : 0,
"ID" : 106,
"auditInfoVersion" : 3435973836,
"createBy" : 2,
"createDate" : "05/18/2012 12:51:39",
"isSubFolder" : 0,
"name" : "new2",
"objectType" : 3,
-->"parentID" : 100,
"policyID" : 2,
"sDescription" : "",
"updateBy" : 2,
"updateDate" : "05/18/2012 12:51:39"
}
]
now the required type of data is:
[{
name:'Piyush1',
children: [{
name: 'New1',
children:[{
name: 'abcasdna'
}]
},{
name: 'New2'
}]
},{
name: 'New'
}]
Now the thing is that there could be any number of folders and any level of hierarchy.
so is there a way i can achieve this conversion?
Related
I'm trying to update show_seats.showByDate.shows.showSeats.seat_details.values.seat_status
this my code .........................................................................................................................................................
db.seats.updateOne({'show_seats.showByDate.shows.showSeats.seat_details.values._id':"62a9e044ec028e60180fe28a"},{$set:{'show_seats.showByDate.shows.showSeats.seat_details.values.seat_status' : true}})
.........................................................................................................................................................
please say what did i wrong**
{
"_id" : ObjectId("62a43ac2d7213c7233cd1dee"),
"totalShowByDay" : "2",
"totalShowDays" : 4,
"movieId" : ObjectId("62953ba3cb6ae625ec9433e6"),
"screenId" : ObjectId("6293b9638dde2d92658d5513"),
"createdAt" : 1654930114438,
"showId" : ObjectId("62a43ac2d7213c7233cd14ed"),
"show_seats" : [
{
"showByDate" : {
"ShowDate" : "2022-06-11",
"shows" : [
{
"showTime" : "2022-06-11T10:00",
"showSeats" : [
[
{
"category" : "CLASSIC",
"seat_details" : [
{
"key" : "A",
"values" : [
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ee"),
"seat_number" : "1",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00",,
{
"_id" : ObjectId("62a43ac2d7213c7233cd14ef"),
"seat_number" : "2",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
},
{
"_id" : ObjectId("62a43ac2d7213c7233cd14f0"),
"seat_number" : "3",
"tag_name" : "A",
"seat_status" : false,
"user_id" : false,
"price" : "140",
"seats_category" : "CLASSIC",
"show_time" : "2022-06-11T10:00"
}
this is what i ended up doing, I need best solution
await db.getDb().collection(coll.seat).aggregate([
{
'$unwind': {
'path': '$show_seats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details'
}
}, {
'$unwind': {
'path': '$show_seats.showByDate.shows.showSeats.seat_details.values'
}
}, {
'$match': {
'show_seats.showByDate.shows.showSeats.seat_details.values._id': ObjectId("62a43ac2d7213c7233cd14ee")
}
}, {
$addFields: { "show_seats.showByDate.shows.showSeats.seat_details.values.seat_status": true }
}
])
I have a database in MongoDB like this
{"productId" : 1,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "500 GB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
},
{"productId" : 2,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "1 TB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
},
{"productId" : 3,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "500 GB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "2.5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
},
{"productId" : 4,
"isVariant": 1,
"variantId" : 1,
"attributeSet" : [
{
"name" : "Capacity",
"value" : "1 TB",
"id" : 3
},
{
"name" : "Form Factor",
"value" : "2.5 inch",
"id" : 4
},
{
"id" : 5,
"name" : "Memory Components",
"value" : "3D NAND"
}
]
}
Now I want to return data where 500 GB has been in productId 1 and 3
The response should be like this:
variantValues : [{
attributeValue : "500 GB",
data : [
{productId : 1},
{productId : 3}
]},
{
attributeValue : "1 TB",
data : [
{productId : 2},
{productId : 4}
]},
{
attributeValue : "2.5 inch",
data : [
{productId : 3},
{productId : 4}
]},
{
attributeValue : "5 inch",
data : [
{productId : 1},
{productId : 2}
]}]
I have the possible values that I store in another collection for variantPossible values. The values that i am storing are like this:
"VariantValues" : {
"3" : [
"500 GB",
"1 TB"
],
"4" : [
"2.5 inch",
"5 inch"
]
},
I want to return the variant values of each product if that product is a variant with the above format. can anyone help me with this.
You should be able to achieve this using $unwind and $group in your aggregation pipeline. This first flattens each attribute into a single document and on those you can group by the attribute value.
Finally, you can use $project to get the desired name for attributeValue:
db.collection.aggregate([
{
$unwind: "$attributeSet"
},
{
$group: {
_id: "$attributeSet.value",
data: {
"$addToSet": {
productId: "$productId"
}
}
}
},
{
"$project": {
_id: 0,
data: 1,
attributeValue: "$_id"
}
}
])
See this simplifed example on mongoplayground: https://mongoplayground.net/p/VASadZnDedc
I have an array of objects.
I need to get an array with unique website name with newest date.
Sample data
"data" : [
{
"position" : 2,
"website" : "abc.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 3,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 1,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-04-06",
"dateTime" : ISODate("2020-04-06T00:00:00.000Z")
},
{
"position" : 6,
"website" : "xyz.agency",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 4,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 2,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 4,
"website" : "opq.com",
"owned" : true,
"date" : "2020-04-01",
"dateTime" : ISODate("2020-04-01T00:00:00.000Z")
}
]
Based on datetTime, position and website. Need a mongoDB query or Javascript code. (Earlier its with dateTime and website)
(Extracting the object from which have highest date and unique website name with top position)
Expected response
"data" : [
{
"position" : 2,
"website" : "abc.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 3,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 6,
"website" : "xyz.agency",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
},
{
"position" : 2,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : ISODate("2020-05-06T00:00:00.000Z")
}
],
use forEach and build an object to handle dulicates and maintain only new date item.
Get Object.values from above object.
const uniq = (arr) => {
const res = {};
arr.forEach((item) => {
if (
!res[item.website] ||
new Date(item.dateTime) > new Date(res[item.website].dateTime)
) {
res[item.website] = { ...item };
}
});
return Object.values(res);
};
// Update uniq2
const uniq2 = (arr) => {
const res = {};
arr.forEach((item) => {
if (!res[item.website]) {
res[item.website] = { ...item };
} else {
if (new Date(item.dateTime) > new Date(res[item.website].dateTime)) {
res[item.website].dateTime = item.dateTime;
}
if (item.position > res[item.website].position) {
res[item.website].position = item.position;
}
}
});
return Object.values(res);
};
const data2 = [
{
position: 2,
website: "abc.com",
owned: false,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 3,
website: "qwe.com",
owned: false,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 1,
website: "qwe.com",
owned: false,
date: "2020-04-06",
dateTime: "2020-04-06T00:00:00.000Z",
},
{
position: 6,
website: "xyz.agency",
owned: false,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 1,
website: "opq.com",
owned: true,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 2,
website: "opq.com",
owned: true,
date: "2020-05-06",
dateTime: "2020-05-06T00:00:00.000Z",
},
{
position: 3,
website: "opq.com",
owned: true,
date: "2020-04-01",
dateTime: "2020-04-01T00:00:00.000Z",
},
];
console.log(uniq2(data2));
In javascript you can reduce it:
var data = [ { "position" : 0, "website" : "abc.com", "owned" : false, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 0, "website" : "qwe.com", "owned" : false, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 0, "website" : "qwe.com", "owned" : false, "date" : "2020-04-06", "dateTime" : 'ISODate("2020-04-06T00:00:00.000Z")' }, { "position" : 0, "website" : "xyz.agency", "owned" : false, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 1, "website" : "opq.com", "owned" : true, "date" : "2020-05-06", "dateTime" : 'ISODate("2020-05-06T00:00:00.000Z")' }, { "position" : 1, "website" : "opq.com", "owned" : true, "date" : "2020-04-01", "dateTime" : 'ISODate("2020-04-01T00:00:00.000Z")' }];
var result = data.reduce((acc, elem)=>{
isPresent = acc.findIndex(k=>k.website==elem.website);
if(isPresent==-1){
acc.push(elem);
} else {
if(new Date(acc[isPresent].date) < new Date(elem.date)) acc[isPresent] = elem;
}
return acc;
},[]);
console.log(result);
Try this- It also takes into consideration when the object is duplicate and dateTime are same/equal :
var d = { "data" : [
{
"position" : 0,
"website" : "abc.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
},
{
"position" : 0,
"website" : "qwe.com",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
},
{
"position" : 0,
"website" : "xyz.agency",
"owned" : false,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
},
{
"position" : 1,
"website" : "opq.com",
"owned" : true,
"date" : "2020-05-06",
"dateTime" : "2020-05-06T00:00:00.000Z"
}
]
}
var filtered = d.data.filter((el,idx)=> {
let found = d.data.findIndex((e,i)=> e.website === el.website && idx !== i);
return found === -1 ? el:Date.parse(el.dateTime) ===
Date.parse((d.data[found]).dateTime) && (found < idx) ? el:
Date.parse(el.dateTime) <
Date.parse((d.data[found]).dateTime) ? el: false
});
console.log(filtered);
I'm actually building a website where I need to display a hierarchy and I've the constraint to build the latter with JSON.
So before implementing anything, two possibilities comes to my mind for the hierarchy and I can't figure out which one will be the most efficient for my project.
Objective
I have to display on a webpage, using HTML5 BoilerPlate, a whole hierarchy represented in the dico_tree field. I'll have to parse this one with JavaScript in order to get all the data it contains.
Constraints
Items can have one or no parent (represented by null).
Items can have multiple or no children.
I'll have to find the fastest way possible each unique item in order to have a quick display on my webpage.
Possibilities I've thought about
Possibility 1
{
"dico_name" : "Dictionary",
"version" : "1",
"dico_tree" : [
{"ID" : 1,"parent" : null,"children" : [2]},
{"ID" : 2, "parent" : 1, "children": [3,4]},
{"ID" : 3, "parent" : 2, "children": null},
{"ID" : 4, "parent" : 2, "children": null},
{"ID" : 5,"parent" : null,"children" : [6]},
{"ID" : 6, "parent" : 5, "children": [7]},
{"ID" : 7, "parent" : 6, "children": null}],
"custom_translations_list" : [
{"TRANSLATION_ID" : 1, "CUSTOM_TRANSLATION_ID" : 12}
}
Possibility 2
{
"dico_name" : "Dictionary",
"version" : "1",
"dico_tree" : [
{"ID" : 1,"parent" : null,"children" : [
{
"ID" : 2, "parent" : 1, "childen": [
{
"ID" : 3, "parent" : 2, "children": null
},
{
"ID" : 4, "parent" : 2, "children": null
}
]
}
]},
{"ID" : 5,"parent" : null,"children" : [
{
"ID" : 6, "parent" : 5, "childen": [
{
"ID" : 7, "parent" : 6, "children": null
}
]
}
]}
],
"custom_translations_list" : [
{"TRANSLATION_ID" : 1, "CUSTOM_TRANSLATION_ID" : 12},
]
}
I'm up to add any precision if you need some !
Thanks for your time and help in advance :).
Does disco_tree have to be an array? Making it an object would allow you to access any item in O(1). Also, if an item with no children has an empty array [] instead of null, you can save yourself some annoying null pointer exceptions.
"dico_tree" : {
1 : {"ID" : 1, "parent" : null, "children" : [2] },
2 : {"ID" : 2, "parent" : 1, "children": [3,4] },
3 : {"ID" : 3, "parent" : 2, "children": [] },
4 : {"ID" : 4, "parent" : 2, "children": [] },
5 : {"ID" : 5, "parent" : null, "children" : [6]},
6 : {"ID" : 6, "parent" : 5, "children": [7] },
7 : {"ID" : 7, "parent" : 6, "children": [] }
},
In MongoDB, if I wanted to express A AND (B OR C), I can easily do by:
db.demo.find( { a: 1, $or: [ {b:1}, {c:1} ] });
But if I wanted (A AND B) OR C -- it does not seem to work. If I try:
db.demo.find( { $or: [ {a:1,b:1}, {c:1} ] });
the above is treating it as A or B or C, which is same as
db.demo.find( { $or: [ {a:1}, {b:1}, {c:1} ] });
At this point in time, I do not want to use Javascript expression $where to accomplish this (but will have no choice, if nothing else works) - the reason being, I am creating a general filter rules, which is being directly converted as MongoDB query.
Any help?
The query works actually, exactly as it should. The question is incorrect.
Here is what a view of what I tried and how it works!
Data:
> db.demo.find()
{ "_id" : 1, "age" : 38, "mgmt" : "no", "salary" : 100 }
{ "_id" : 2, "age" : 45, "salary" : 200, "mgmt" : "no" }
{ "_id" : 3, "age" : 50, "salary" : 250, "mgmt" : "no" }
{ "_id" : 4, "age" : 51, "salary" : 75, "mgmt" : "yes" }
{ "_id" : 5, "age" : 45, "salary" : 75, "mgmt" : "no" }
Query to get (Age>40 AND Salary>200) OR mgmt='yes'
> db.demo.find( { $or: [ { age: {$gt:40}, salary:{$gt:200} }, {mgmt: 'yes'} ] })
{ "_id" : 3, "age" : 50, "salary" : 250, "mgmt" : "no" }
{ "_id" : 4, "age" : 51, "salary" : 75, "mgmt" : "yes" }
Query to get (Age>40) OR (Salary>200) OR mgmt='yes'
> db.demo.find( { $or: [ { age: {$gt:40}}, {salary:{$gt:201} }, {mgmt: 'yes'} ] })
{ "_id" : 2, "age" : 45, "salary" : 200, "mgmt" : "no" }
{ "_id" : 3, "age" : 50, "salary" : 250, "mgmt" : "no" }
{ "_id" : 4, "age" : 51, "salary" : 75, "mgmt" : "yes" }
{ "_id" : 5, "age" : 45, "salary" : 75, "mgmt" : "no" }
All of them working as it should. Great!