Hide nested fields with "name" in $project mongodb - mongodb-query

This is the document available that needs to be projected by hiding all the "name" fields in the document. Not sure if we have any specific way in mongo aggregation framework to search for all the nested fields with name - "name" and hide it. Please suggest
"group1":{
"field1":{
"name":"a"
},
"field2":{
"data":{
"name":"b"
}
},
"group2":{
"field2":{
"name":"a"
},
"field3":{
"data":{
"name":"b"
}
}
}
}
}
Expected output:
{
group1: {
field1: {
}, "field2": {
"data": {
}
},
"group2": {
"field2": {
}, "field3": {
"data": {
}
}
}
}
}

Related

mongodb aggregation - setting the contents of array as input, while storing sibling keys

I'm in the middle of an aggregation pipeline when I have the following data passed from the previous step as input to a "count all leaves in all branches/trees/forests" as indicated in this question: Need guidance on mongo aggregate lookup to count subnodes in all child nodes for each node
{
forest_ranger: "bob",
some_other_data: {},
forests: [
{
forest_id: 'forestA',
trees: [
{
tree_id: 'treeA',
branches: [
{
branch_id: 'branchA',
}
]
}
]
}
],
}
I have that all working properly, but only if I'm reading directly from a collection.
My question is - how can I
a) save forest_ranger and some_other_data "for later"
b) and start the next stage by mapping forests as the array of
documents that are input to the "count all leaves"?
Note - I tried to get just (b) working for starters, but I can't seem to treat "forests" as an array so that the solution in the link above can do its magic.
Then- when that phase is done, extract what was stored in (a) to get the original format back with all the counts.
That is the main disadvantage of using $group. You have to explicitly carry forward every non participating fields using either $first or $last like this:
Get the test data with those additional fields from this new MongoPlayground link.
Check the updated query taken from the link:
db.Forests.aggregate([
{ $unwind: "$forests" },
{ $unwind: "$forests.trees" },
{ $unwind: "$forests.trees.branches" },
{
$lookup: {
from: "Leaves",
localField: "forests.trees.branches.branch_id",
foreignField: "branch_id",
as: "forests.trees.branches.leaves"
}
},
{
$addFields: {
"forests.trees.branches.leaf_count": { $size: "$forests.trees.branches.leaves" }
}
},
{
$project: { "forests.trees.branches.leaves": 0 }
},
{
$group: {
_id: {
forest_id: "$forests.forest_id",
tree_id: "$forests.trees.tree_id"
},
forest_ranger: { $first: "$forest_ranger" },
some_other_data: { $first: "$some_other_data" },
leaf_count: { $sum: "$forests.trees.branches.leaf_count" },
branches: { $push: "$forests.trees.branches" }
}
},
{
$group: {
_id: "$_id.forest_id",
forest_ranger: { $first: "$forest_ranger" },
some_other_data: { $first: "$some_other_data" },
leaf_count: { $sum: "$leaf_count" },
trees: {
$push: {
leaf_count: { $sum: "$leaf_count" },
tree_id: "$_id.tree_id",
branches: "$branches"
}
}
}
},
{
$group: {
_id: null,
forest_ranger: { $first: "$forest_ranger" },
some_other_data: { $first: "$some_other_data" },
leaf_count: { $sum: "$leaf_count" },
forests: {
$push: {
leaf_count: { $sum: "$leaf_count" },
forest_id: "$_id",
trees: "$trees"
}
}
}
},
{
$project: { _id: 0 }
}
])
Output:
{
"forest_ranger" : "bob",
"some_other_data" : {
"data" : "Dummy data"
},
"leaf_count" : 4,
"forests" : [
{
"leaf_count" : 3,
"forest_id" : "forestA",
"trees" : [
{
"leaf_count" : 1,
"tree_id" : "treeB",
"branches" : [
{
"branch_id" : "branchB",
"leaf_count" : 1
}
]
},
{
"leaf_count" : 2,
"tree_id" : "treeA",
"branches" : [
{
"branch_id" : "branchA",
"leaf_count" : 1
},
{
"branch_id" : "branchA1",
"leaf_count" : 1
},
{
"branch_id" : "branchA2",
"leaf_count" : 0
}
]
}
]
},
{
"leaf_count" : 1,
"forest_id" : "forestB",
"trees" : [
{
"leaf_count" : 0,
"tree_id" : "treeD",
"branches" : [
{
"branch_id" : "branchD",
"leaf_count" : 0
}
]
},
{
"leaf_count" : 1,
"tree_id" : "treeC",
"branches" : [
{
"branch_id" : "branchC",
"leaf_count" : 1
}
]
}
]
},
{
"leaf_count" : 0,
"forest_id" : "forestC",
"trees" : [
{
"leaf_count" : 0,
"tree_id" : "treeE",
"branches" : [
{
"branch_id" : "branchE",
"leaf_count" : 0
}
]
}
]
}
]
}

How to get the correct object in an nested array in Vue.js?

I use Axios to display a JSON data and I have succeeded. But I want to show an object based on date and time, it shows now all data and I need to filter it.
So I want to look at today's date and show the object based on that, so I want to show the next upcoming event. (24/05/2020)
What I currently have:
Json:
{
"doc": [
{
"data": {
"events": {
"18807612": {
"_dt": {
"_doc": "time",
"time": "18:45",
"date": "14/05/20",
"tz": "UTC",
"tzoffset": 0,
"uts": 1566067500
},
"week": 33,
"teams": {
"home": {
"name": "name 1",
"mediumname": "name1",
"uid": 3014
},
"away": {
"name": "name 2",
"mediumname": "name 2",
"uid": 3020
}
}
},
"18807618": {
"_dt": {
"_doc": "time",
"time": "18:45",
"date": "24/05/20",
"tz": "UTC",
"tzoffset": 0,
"uts": 1566067500
},
"week": 33,
"teams": {
"home": {
"name": "name 1",
"mediumname": "name1",
"uid": 3014
},
"away": {
"name": "name 2",
"mediumname": "name2",
"uid": 3020
}
}
}
}
}
}
]
}
Store:
async loadPosts({ commit }) {
// Define urls pages
const urlEvents = 'http://api.example.com/302020';
// Set pages
const [
responseEvents
] = await Promise.all([
// Responses pages
this.$axios.get(urlEvents)
]);
// variables pages
this.events = responseEvents.data.doc[0].data.events
// mutations pages
commit('SET_EVENTS', this.events)
}
},
mutations: {
SET_EVENTS (state, events) {
state.events = events;
}
}
And to show the data I use this:
import {mapState} from 'vuex';
export default {
name: 'NextMatch',
mounted() {
this.$store.dispatch('loadPosts')
},
computed: {
...mapState([
'events'
])
}
}
<h1>{{events}}</h1>
But this shows all data, and what I try to get is the first upcoming event for the object with the "uid": 3014.
So I want to show the date, time and names of the home and away team.
How can I get the correct data by filtering the data?
Something like this or similar to this should work:
In your Vue component's <template>:
`<h1>{{selectedEvent._dt.date}}</h1>`
In your Vue component's <script>:
props: {
eventID: {
type: Number,
default: 3014
},
},
computed: {
...mapState([
'events'
]),
eventsArr(){
if (!this.events) return {} //make sure Object.entries gets object.
return Object.entries(this.events)
},
selectedEvent(){
const selectedArr = this.eventsArr.find(([eID, e]) => e.teams.home.uid === this.eventID)
return selectedArr[1]
}
}

Vue Change state in forEach loop and if condition to update replies of any comment

I have a State with editedIndex i want to change it in forEach loop but i am not able to call it in that loop.
i have done this code so far
data() {
return {
dialog: false,
comments: [],
editedReply: {
reply: null,
comment_id: null,
name: JSON.parse(localStorage.getItem("user")).name,
email: JSON.parse(localStorage.getItem("user")).email
},
editedIndex: -1
};
},
in above code i have added the initial state of comments which contains index of all comments
I am trying to update the replies of each comment.
handleEdit(reply) {
let commentArray = this.comments;
commentArray.forEach(function(comment) {
comment.reply.forEach(function(item) {
if (reply.id === item.id) {
this.editedIndex = comment.reply.indexOf(item);
this.editedReply = Object.assign({}, item);
}
});
});
this.dialog = true;
},
from i have the list of comments in this.comments and reply represents the reply on which i have clicked to update.
my problem here is i am not able to call this.editedIndex and this.editedReply in if condition as i have mentioned above.
I have used comment.reply every comment contains a array of reply which you can see in below json data for comment i want to update the this json reply
json data for my comments is
{
"comments": [
{
"id": 5,
"comment": "nice blog",
"name": "test user",
"email": "dhruvil#gkmit.co",
"status": true,
"created_at": "2020-05-28T04:36:46.797Z",
"article": {
"id": 308,
"title": "test for comment article"
},
"reply": [
{
"id": 99,
"reply": "abcbc",
"name": "test2",
"email": "test2#mailinator.com",
"created_at": "2020-05-29T13:23:31.358Z"
},
{
"id": 100,
"reply": "abcbc",
"name": "test2",
"email": "test2#mailinator.com",
"created_at": "2020-05-29T13:23:31.521Z"
},
]
},
{
.......... and so on
},
]
}
Try using this snippet for handleEdit method:
handleEdit(reply) {
let commentArray = this.comments;
commentArray.forEach(comment => {
comment.reply.forEach(item => {
if (reply.id === item.id) {
this.editedIndex = comment.reply.indexOf(item);
this.editedReply = Object.assign({}, item);
}
});
});
this.dialog = true;
},
What's different here is I used arrow functions for the callbacks of forEach operations.

Curve Line Graphic and Jsonfile

I realized this curve line graph using d3 and nvd3:
d3.json('/Models/Scripts/test_data2.json', function(data) {
nv.addGraph(function() {
var chart = nv.models.lineWithFocusChart();
// chart.transitionDuration(500);
chart.xAxis
.tickFormat(d3.format(',f'));
chart.x2Axis
.tickFormat(d3.format(',f'));
chart.yAxis
.tickFormat(d3.format(',.2f'));
chart.y2Axis
.tickFormat(d3.format(',.2f'));
d3.select('#chart svg')
.datum(data)
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
});
The question is, how should I write my json file to recognize different curves?
If I have the following json it doesn't work with two or more lines. I did several tries.
[{"CurveArray":[{
"Line1":[
{
"x":-52482.23046875,
"y":0.0
},
{
"x":-51380.384892578128,
"y":0.4
},
{
"x":-50278.539316406248,
"y":2
},
{
"x":-49176.693740234376,
"y":0.9
},
{
"x":-48074.848164062496,
"y":0.7
}
]},
"Line2":[
{
"x":-52482.23046875,
"y":0.0
},
{
"x":-51380.384892578128,
"y":0.4
},
{
"x":-50278.539316406248,
"y":2
},
{
"x":-49176.693740234376,
"y":0.9
},
{
"x":-48074.848164062496,
"y":0.7
}
]}
]}]
I tried to modify so:
It doesn't work again.
[
{
key: "Line1",
value: [
{
"x":-52482.23046875,
"y":0.0
},
{
"x":-51380.384892578128,
"y":0.4
},
{
"x":-50278.539316406248,
"y":2
},
{
"x":-49176.693740234376,
"y":0.9
},
{
"x":-48074.848164062496,
"y":0.7
}
]
},
{
key: "Line2",
value: [
{
"x":-50000,
"y":0.0
},
{
"x":-59000,
"y":0.4
},
{
"x":-48000,
"y":2
},
{
"x":-49100,
"y":0.9
},
{
"x":-49800,
"y":0.7
}
]
}
]

How do I correctly load data into a Picker with an ArrayDataModel? (Blackberry OS 10 Cascades)

I am working with Blackberry 10 Cascades and I am trying to load data into a picker using an ArrayDataModel. All of the documentation online I have read thusfar have given me examples to use with XMLDataSource, however since my data is JSON and coming from a live feed I would rather not have to take the performance hit of serializing it into XML to load into the XMLDataSource (which seems to be pretty temperamental from my cursory investigations).
I have some code that is giving me partial functionality (correct number of rows and columns), however when attempting to access the pickerItemData property, the result is always undefined.
The following code shows the partial functionality, it is adapted from http://developer.blackberry.com/native/reference/cascades/bb_cascades_picker.html
There is some debug output from the Console which reads
Picker: incorrect model, wrong columns number: 0
Unfortunately this doesn't make any sense to me, as the Picker is rendering the correct number of columns. There also does not appear to be any documentation on the error message.
import bb.cascades 1.2
Page {
Container {
leftPadding: 24.0
rightPadding: 24.0
topPadding: 12.0
bottomPadding: 12.0
controls: [
Picker {
id: picker
title: "Picker title"
expanded: true
rootIndexPath: []
dataModel: ArrayDataModel {
id: dataModel
}
pickerItemComponents: [
PickerItemComponent {
type: "item1"
content: Container {
background: Color.create(pickerItemData.background)
}
},
PickerItemComponent {
type: "item2"
content: Container {
Label {
text: pickerItemData.text
textStyle.color: Color.create(pickerItemData.color)
}
}
}
]
onCreationCompleted: {
dataModel.append([
{
"item1": [ {
"background": "#ff0000ff"
}, {
"background": "#ff00ff00"
},
{
"background": "#ffff0000"
}, {
"background": "#ff00ffff"
} ]
},
{
"item2": [
{
"text": "Item 1",
"color": "#ff888888"
},
{
"text": "Item 2",
"color": "#ff0000ff"
},
{
"text": "Item 3",
"color": "#ff00ff00"
},
{
"text": "Item 4",
"color": "#ff00ffff"
},
{
"text": "Item 5",
"color": "#ffff0000"
},
{
"text": "Item 6",
"color": "#ffff00ff"
},
{
"text": "Item 7",
"color": "#ffffff00"
},
{
"text": "Item 8",
"color": "#ffffffff"
} ]
} ]);
}
onSelectedValueChanged: {
var index0 = picker.selectedIndex(0);
var index1 = picker.selectedIndex(1);
console.log("Selection: " + index0 + ", " + index1);
}
}
]
}
}
The structure of the data provided via JSON is identical to the structure of the data provided by the XMLDataSource. Any help or insight into this problem would be greatly appreciated.