I'm using mongoose on node js and I'm having a Page collection such as
{
"_id" : ObjectId("5b3cf0e7ee00450156711a47"),
"language" : "en",
"published" : true,
"content" : [
{
"title" : "my title 1",
"subTitle" : "subtitle 1",
"items" : [
{
"title" : "my item title 1",
"subTitle" : "item subtitle 1",
},
{
"title" : "my item title 2",
"subTitle" : "item subtitle 2",
}
]
},
{
"title" : "my title 2",
"subTitle" : "subtitle 2",
}
],
"createdAt" : ISODate("2018-07-04T16:08:07.057Z"),
"__v" : 0
}
The "content" array includes many objects and some of those object include an "items" array that includes many object too. (nested on 2 level deep)
I was wondering if it wouldn't be better to reference content object instead of having them nested, because they're having nested documents too (inside items) so it's a 2 level deep nesting.
I have to use page.content.id(listId).items.id(id) in order to find them and page.content.id(listId).items.id(id)[key] = value; page.save(); to update them.
Considering there shouldn't have more than 3 "items" and 5 "content" maximum, what do you think would be the best solution, updating nested on 2 level deep document or content having a reference/collection?
I also plan to do a versioning which would influence this decision, as updating could/would create a new version of the document.
I would prefer reference collection here
Page Schema
{
"language" : String,
"published" : Boolean,
"content" : [
{type: mongoose.Schema.Types.ObjectId, ref: 'content'}
],
"createdAt" : Date,
}
Content Schema
{
"title" : String,
"subTitle" : String,
"items": [{type: mongoose.Schema.Types.ObjectId, ref: 'items'}]
}
Items Schema
{
"title" :String,
"subTitle" : String,
}
Related
My collection looks like this:
> db.projects_columns.find()
{ "_id" : "5b28866a13311e44a82e4b8d", "checkbox" : true }
{ "_id" : "5b28866a13311e44a82e4b8e", "field" : "_id", "title" : "ID", "sortable" : true }
{ "_id" : "5b28866a13311e44a82e4b8f", "field" : "Project", "title" : "Project", "editable" : { "title" : "Project", "placeholder" : "Project" } }
{ "_id" : "5b28866a13311e44a82e4b90", "field" : "Owner", "title" : "Owner", "editable" : { "title" : "Owner", "placeholder" : "Owner" } }
{ "_id" : "5b28866a13311e44a82e4b91", "field" : "Name #1", "title" : "Name #1", "editable" : { "title" : "Name #1", "placeholder" : "Name #1" } }
{ "_id" : "5b28866a13311e44a82e4b92", "field" : "Name #2", "title" : "Name #2", "editable" : { "title" : "Name #2", "placeholder" : "Name #2" } }
{ "_id" : "5b28866a13311e44a82e4b93", "field" : "Status", "title" : "Status", "editable" : { "title" : "Status", "type" : "select", "source" : [ { "value" : "", "text" : "Not Selected" }, { "value" : "Not Started", "text" : "Not Started" }, { "value" : "WIP", "text" : "WIP" }, { "value" : "Completed", "text" : "Completed" } ], "display" : "function (value, sourceData) { var colors = { 0: 'Gray', 1: '#E67C73', 2: '#F6B86B', 3: '#57BB8A' }; var status_ele = $.grep(sourceData, function(ele){ return ele.value == value; }); $(this).text(status_ele[0].text).css('color', colors[value]); }", "showbuttons" : false } }
You can see that in the very last document that I have stored a function as text.Now the idea is that I will request this data and will be in an Javascript Array format.
But I want to be able to have my function without the quotes! You can see that simply evaluating it will not work because I need to have it still needs to be inside of the object ready to be executed when the array is used.
How can I achieve this?
Thanks for any help!
There are two possible solutions, but neither particularly safe and you should strongly consider why you need to store functions as strings in the first place. That being said, you could do two things.
The simplest is to use eval. To do so, you would have to first parse the object like normal, and then set the property that you want to the result of eval-ing the function string, like so:
// Pass in whatever JSON you want to parse
var myObject = JSON.parse(myJSONString);
// Converts the string to a function
myObject.display = eval("(" + myObject.display + ")");
// Call the function with whatever parameters you want
myObject.display(param1, param2);
The additional parentheses are to make sure that evaluation works correctly. Note, that this is not considered safe by Mozilla and there is an explicit recommendation not to use eval.
The second option is to use the Function constructor. To do so, you would need to restructure your data so that you store the parameters separately, so you could do something like this:
var myObject = JSON.parse(myJSONString);
// displayParam1 and displayParam2 are the stored names of your parameters for the function
myObject.display = Function(myObject.displayParam1, myObject.displayParam2, myObject.display)
This method definitely takes more modification, so if you want to use your existing structure, I recommend eval. However, again, make sure that this is absolutely necessary because both are considered unsafe since outside actors could basically inject code into your server.
I'm having some trouble determining how to find a document within a collection, and a value within an object in a subarray of that document — and then update a value within an object in that array.
I need to do the following:
find by _id
find object in ratings array that matches the user + post keys
update the report value within that object
For example, the documents in my collection are set up like below.
{
"_id" : "mz32AcxhgBLoviRWs",
"ratings" : [
{
"user" : "mz32AcxhgBLoviRWs",
"post" : "SMbR6s6SaSfsFn5Bv",
"postTitle" : "fdsfasdf",
"date" : "2017-09-27",
"rating" : "4",
"review" : "sdfa",
"report" : "a report"
},
{
"user" : "mz32AcxhgBLoviRWs",
"post" : "iZbjMCFR3cDNMo57W",
"postTitle" : "today",
"date" : "2017-09-27",
"rating" : "4",
"review" : "sdfa",
"report" : "some report"
}
]
}
It seems that you want just one update, not three separated queries.
Collection.update({
_id: <id>,
ratings: {
$elemMatch: {
user: <user>,
post: <post>
}
}
}, {
$set: {
'ratings.$.report': <report>
}
});
Documentation: $elemMatch, <array>.$.
I'm using a model tree structure for my collection. As references I'm using parent-fields. I need to get attributes from the current object and all its parents. The last element in a path has a field 'target'. So I start with
var result = parent = Articles.findOne({target: this.params._id});
do {
parent = Articles.findOne({_id: parent.parent}).parent;
for (var attrname in parent) { result[attrname] = parent[attrname]; }
}
while (parent.parent === null);
That seems to be very inefficient to me. Isn't it possible to do that with one line to get an object with all elements? Then I could process that object.
Example documents
{
"_id" : "LD6h5ZcDuJjexfKfx",
"title" : "title",
"publisher" : "public",
"author" : "author"
}
{
"_id" : "KSiyh8zHRq8RZQ2E6",
"edition" : "edition",
"year" : "2020",
"parent" : "LD6h5ZcDuJjexfKfx"
}
{
"_id" : "5yCk4y25wrLBLZhyY",
"pageNumbers" : "1-10",
"target" : "9sjhzPhyTuQ5Kbh6v",
"parent" : "KSiyh8zHRq8RZQ2E6"
}
So starting with "target" : "9sjhzPhyTuQ5Kbh6v" I would like to get the two parent documents (in this example).
At least I need the dataset
"title" : "title",
"publisher" : "public",
"author" : "author",
"edition" : "edition",
"year" : "2020",
"pageNumbers" : "1-10"
If you want to do this in a single query then you need to follow the array of ancestors pattern in Mongodb. Otherwise you need to recursively traverse the branches above the leaf node as you are doing. For hierarchies with low depth such as yours this is not a big penalty.
With an array of ancestors your doc tree would look like:
{
"_id" : "LD6h5ZcDuJjexfKfx",
"title" : "title",
"publisher" : "public",
"author" : "author",
}
{
"_id" : "KSiyh8zHRq8RZQ2E6",
"edition" : "edition",
"year" : "2020",
"ancestors" : ["LD6h5ZcDuJjexfKfx"],
"parent" : "LD6h5ZcDuJjexfKfx"
}
{
"_id" : "5yCk4y25wrLBLZhyY",
"pageNumbers" : "1-10",
"target" : "9sjhzPhyTuQ5Kbh6v",
"ancestors" : ["LD6h5ZcDuJjexfKfx","KSiyh8zHRq8RZQ2E6"],
"parent" : "KSiyh8zHRq8RZQ2E6"
}
To get the doc and its parents:
Articles.find({ $or: [ { target: target },
_id: { $in: Articles.findOne({ target: target }).ancestors }]});
I have a collection like User having list user and that user have list of user. Like hierarchy.
{
"_id" : ObjectId("55530326bc687d21783fd1ff"),
"Name" : "User 1",
"Role" : "Manager",
number:NumberLong(0),
"1" :
[
{
"_id" : ObjectId("55530326bc687d21783fd1fd"),
"Name" : "User 2",
"Role" : "Ass Manager",
number:NumberLong(0),
"1" :
[
.......
]
}
{
"_id" : ObjectId("55530326bc687d21783fd1fq"),
"Name" : "User 2",
"Role" : "Ass Manager",
number:NumberLong(1),
"1" :
[
.........
]
},
{
"_id" : ObjectId("55530326bc687d21783fd1fg"),
"Name" : "User 3",
"Role" : "Ass Manager",
number:NumberLong(2),
"1" :
[
........
]
}
],
"2" :
[
{
"_id" : ObjectId("55530326bc687d21783fd1fw"),
"Name" : "User 4",
"Role" : "Specialist",
number:NumberLong(0),
"1" :
[
.......
]
}
{
"_id" : ObjectId("55530326bc687d21783fd1fe"),
"Name" : "User 5",
"Role" : "Specialist",
number:NumberLong(1),
"1" :
[
.........
]
},
{
"_id" : ObjectId("55530326bc687d21783fd1fr"),
"Name" : "User 6",
"Role" : "Specialist",
number:NumberLong(2),
"1" :
[
........
]
}
]
}
The above is just one collection for sample, like this i have nearly 10000 document. I need to find the collection which have 'number' as 0. Even if any one embedded document have 'number' as 0 I want that document.
Note : I can't tell how many child will come for a user.
All right, I will assume that each of your User documents can have two arrays of children users (namely "1" and "2"), and that you have a maximum nesting level, say 3 (This means that a nested user cannot have more than 2 anchestors). By the way, the maximum nesting level allowed by mongodb is 100.
Probably this is not want you wanted: in this case you have issues with your schema design, because
An arbitrarily nested schema is not good
If keys are intended as indexes, you should consider using an array instead of an object (click here for a similar case).
Now, let's pretend that my assumptions were ok for you. Try (I'm calling your collection users since we don't usually capitalize collection names):
db.users.find({$or:["1.number" : 0, "2.number" : 0, "1.1.number":0, "1.2.number":0, ..., "2.2.1.number":0, "2.2.2.number":0]})
I have skipped some combinations that you need to add. Notice that you don't need to worry about the position in arrays, and that with only 3 levels of nesting there are already quite many clauses for the $or operator, and that will possibly convince you that it's better to follow the linked best practices.
NOTE for future readers: the OP has clarified in a comment that he doesn't actually need an update, but a find query.
I pass some json data in jstree and then I update the tree.
For example my initial data is
[
{ "id" : "demo_root_1", "text" : "Root 1", "children" : true, "type" : "root" },
{ "id" : "demo_root_2", "text" : "Root 2", "type" : "root" }
]
I update it a little (simple rename). "Root 1" becomes "UPDATED". How can I get the updated version of my data in json format like below?
[
{ "id" : "demo_root_1", "text" : "UPDATED", "children" : true, "type" : "root" },
{ "id" : "demo_root_2", "text" : "Root 2", "type" : "root" }
]
Okay..so basically if you want to refresh the tree with the updated data, then after the updation code, call the jstree callback:
$(tree).jstree('refresh');
This will refresh the tree with the new json data.
let me know in case of further issues.