I have the following model attributes:
[{
"id": 1,
"details": {
"name": "Sun Tzu",
"height": "180",
},
"lists": [
[{
"coworkers": "company cool",
"friends": "School",
}],
[{
"coworkers": "company nice",
"friends": "Childhood",
}]
]
}]
Yes, I know it is confusing but I am trying to understand nested models.
I want to display in a view (a table row), all the friends of id:1 model.
For example: School, Childhood.
How do I do that?
Thanks in advance!
var friends = _.chain(data)
.findWhere({ id: 1 })
.result('lists')
.flatten(false)
.pluck('friends')
.value();
You can chain functions to get the output you are looking for
console.log(_.chain(data)
.find(function(currentObject) {
return currentObject.id === 1;
})
.pick("lists")
.flatten(false)
.pluck("friends")
.value());
Output
[ 'School', 'Childhood' ]
Related
I'm using ng2-tree https://angular2-tree.readme.io/v3.2.0/docs/inputs plugin
When i input below json it is showing as undefined
[
{
"value": "helper",
"name": "helper",
"children": []
},
{
"value": "taxi",
"name": "taxi",
"children": []
},
{
"value": "Cake",
"name": "Cake",
"children": [
{
"name": "Chocolate Fudge Cake",
"value": "Chocolate Fudge Cake"
},
{
"name": "Carrot & Walnut Cake",
"value": "Carrot & Walnut Cake"
}
]
}
]
with above json my result is as undefined you can see them in my provided link below
here is the stackblitz link: https://stackblitz.com/edit/angular-ng2-tree-aouyza?file=app/app.component.ts
Please help me thanks in advance!!
Your data structure is wrong. The tree component received as input param a TreeModel and you're having an array of TreeModels at the moment.
Either you adjust your data structure and use a parent TreeModel to wrap your current ones as its children, like following:
tree: TreeModel = {
value: 'Parent Model',
children: [
{
value: 'helper',
name: 'helper',
children: [],
},
{
value: 'taxi',
name: 'taxi',
children: [],
},
{
value: 'Cake',
name: 'Cake',
children: [
{
name: 'Chocolate Fudge Cake',
value: 'Chocolate Fudge Cake',
},
{
name: 'Carrot & Walnut Cake',
value: 'Carrot & Walnut Cake',
},
],
}
]
};
Or you iterate over the array in the HTML and use multiple tree components. That would look like following:
<tree [tree]="t" *ngFor="let t of tree"></tree>
For more information see the Github page of ng2-tree ;)
Update:
You still need to adjust the data model the way I suggested but you can hide the empty root node. To do so, you need to do following:
HTML
<tree [tree]="tree" [settings]="{ rootIsVisible: false }"></tree>
Due to this setting a class rootless is applied which hides the empyt root node but only if you've added node_modules/ng2-tree/styles.css to your angular.json or you've added a custom implementation for that class.
You can find the settings doc here.
I am new to mongodb and im trying to (as title says) "Given an array of matches, find all documents that have atleast one match and replace all matches with given value"
For example, lets say i have those documents in my mongodb:
[
{
"_id": ObjectId("5e90880a39cfeaaf0b97b576"),
"StudentName": [
"Chris, C",
"David, O",
"Mike, K",
"Sam, Bard"
]
},
{
"_id": ObjectId("5e90880a39cfeaaf0b97b577"),
"StudentName": [
"Joe, J",
"John, K",
"David, O",
"Sam, Ba rd",
"Sam, B"
]
}
]
And i want to find all documents that contains either ["Sam, B", "Sam, Bard", "Sam, Ba rd"] and replace with "Sam"
Expected result:
[
{
"_id": ObjectId("5e90880a39cfeaaf0b97b576"),
"StudentName": [
"Chris, C",
"David, O",
"Mike, K",
"Sam"
]
},
{
"_id": ObjectId("5e90880a39cfeaaf0b97b577"),
"StudentName": [
"Joe, J",
"John, K",
"David, O",
"Sam",
"Sam"
]
}
]
What i tried to do:
db.collection.updateMany({ "StudentName": {"$in":["Sam, B", "Sam, Bard", "Sam, Ba rd"]} },{ "$set": { "StudentName.$": "Sam" } })
Which didn't work. Any help? Thank you very much.
EDIT1: I need it to be dynamic, i'll be providing the array of possibles matches and the string to replace with through a NodeJS application.
EDIT2: To give an example for EDIT1, i could pass an array like this ["John,"Bob","Jessica","Robert"] to replace with "Josh"
There are several ways you can do this. By the looks of it you want this to be done via one command that can be run directly in the shell.
You can do this leveraging arrayFilters option within updateMany. See https://docs.mongodb.com/manual/reference/method/db.collection.updateMany/#std-label-updateMany-arrayFilters for further documentation on it.
For simplicity I won't leverage indices so the below command would iterate over all the documents in the collection. If you want to leverage an index you would just adjust the query portion of the updateMany
db.collection.updateMany(
{ },
{ $set: { "StudentName.$[element]" : "Sam" } },
{ arrayFilters: [ { "element": /.*Sam.*/i } ] }
)
The above will replace any value that contains "Sam" with the value "Sam". So "Sam I Am" would be replaced with "Sam".
If you know the values you are trying to replace:
db.students.updateMany(
{ },
{ $set: { "StudentName.$[element]" : "Ana" } },
{ arrayFilters: [ { "element": { $in: ["John", "Jessica", "Robert", "Rob"] } } ] }
)
which would replace all values of John, Jessica, Robert and Rob with "Ana".
I have an array shown as below. I want to know which values inside departure and arrival fields.
Array :
var data = {
"origin": "Antalya",
"destination": "IST",
"flights": [{
"provider": "thy",
"time": "2017-07-07 10:30",
"legs": [{
"departure": "AYT",
"arrival": "IST"
}]
},{
"provider": "thy",
"time": "2017-07-07 14:30",
"legs": [{
"departure": "AYT",
"arrival": "ESB"
},{
"departure": "ESB",
"arrival": "IST"
}]
},{
"provider": "pegasus",
"time": "2017-07-07 06:30",
"legs": [{
"departure": "AYT",
"arrival": "ADB"
},{
"departure": "ADB",
"arrival": "IST"
}]
}]
};
I want to new array like this :
["AYT","IST","ESB","ADB"]
How can i handle it using lodash?
Here's a solution using lodash:
let result = _(data.flights)
.flatMap('legs')
.flatMap(_.values)
.uniq()
.value();
First we get a flattened array of legs, transform that into a flattened array of the values of the properties of each leg, before finally getting the unique values.
Well loop through your data and create a string array, and then use the uniq function, like:
var data = {"origin":"Antalya","destination":"IST","flights":[{"provider":"thy","time":"2017-07-07 10:30","legs":[{"departure":"AYT","arrival":"IST"}]},{"provider":"thy","time":"2017-07-07 14:30","legs":[{"departure":"AYT","arrival":"ESB"},{"departure":"ESB","arrival":"IST"}]},{"provider":"pegasus","time":"2017-07-07 06:30","legs":[{"departure":"AYT","arrival":"ADB"},{"departure":"ADB","arrival":"IST"}]}]};
var legs = [];
_.each(data.flights, flight => {
_.each(flight.legs, leg => {
legs.push(leg.departure);
legs.push(leg.arrival);
});
});
console.log(_.uniq(legs));
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
Given this collection:
[{
"users": [{
"name": "one"
}, {
"name": "two"
}]
}, {
"users": [{
"name": "one"
}, {
"name": "three"
}]
}, {
"users": [{
"name": "fifteen"
}, {
"name": "one"
}]
}]
How can I query this using values (ie, "one" and "two") so that the findOne method returns only the document that has both "name":"one"and "name":"two" (order not relevant)? The users array will always have 2 elements, no more, no less.
I was trying something along the lines of:
Collection.findOne({"users":{$all:["one", "two"]}})
But it isn't working. Can anyone help?
EDIT: Latest attempt:
Collection.findOne({"users":{"name": {$all:["one","two"]}}})
Try this one:
{"users": {$all: [{"name": "one"}, {"name": "two"}]}}
Or use dot notation as proposed by JohnnyHK.
See here how $all is used: https://docs.mongodb.org/manual/reference/operator/query/all/
EDIT: Data was changed.
You can do this by using dot notation to identify a specific field within the array to which the $all operator should be applied:
Collection.findOne({'users.name': {$all: ['one', 'two']}})
I'm trying to figure out how to format this json in angular this is the result from extending all models from multiple forms.
{
"identifications": {},
"insurances": [
{
"insurances": [
{
"category": "",
"compName": "",
"planType": "",
"note": ""
}
]
}
],
"medical": {
"doctors": [
{
"doctors": [
{
"firstname": "James"
"note": "James Bond Note"
},
{
"firstname": "Robin",
"note": "Lorem Ipsum Dolor"
}
]
}
],
"records": {}
}
Here's I need to achieve to insert it into the API.
{
"parent_id": 17,
"insurances": [{
"insurance_type": "",
"notes": ""
}],
"medical": {
"doctors": {},
"blood": {},
},
"records": {}
}
If you're really looking at just pretty-printing stuff, investigate JSON.stringify:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify
If you're trying to change the object from one data representation to another (I'll note that the second object is structurally different from the first, not just a matter of whitespace), then you just need to write some javascript that does stuff like new.doctors = old[0].medical[1].doctors (example, not actual code)
This question is pretty old, but nowadays you should use the json filter. For example:
var data = {a: 1, b: 2};
Then in the template:
{{ data|json:2 }}
Should output
{
a: 1,
b: 2
}