Every update fails for me. I'm not sure what I'm doing wrong:
await search
.update({
index: 'posts',
id: post._id,
body: post,
})
.catch(err => {
failed.updates += 1;
console.log('update failed', post._id);
});
Here is how the docs got inserted:
await search
.index({
index: 'posts',
id: post._id,
body: post,
})
.catch(err => {
failed.inserts += 1;
console.error(err);
});
Inserts work flawlessly. I'm wondering if nothing changed maybe update fails by default? Not sure why but they all fail.
Here is the error:
{
"name": "ResponseError",
"meta": {
"body": {
"error": {
"root_cause": [
{
"type": "x_content_parse_exception",
"reason": "[1:2] [UpdateRequest] unknown field [score]"
}
],
"type": "x_content_parse_exception",
"reason": "[1:2] [UpdateRequest] unknown field [score]"
},
"status": 400
},
"statusCode": 400,
"headers": {
"content-type": "application/json; charset=UTF-8",
"content-length": "221"
},
"warnings": null,
"meta": {
"context": null,
"request": {
"params": {
"method": "POST",
"path": "/posts/_update/5ea1947ebbb15d001c408c4d",
"body": "{\"score\":2,\"views\":21,\"type\":\"link\",\"ranking\":2.7183885087703956,\"sponsored\":false,\"title\":\"Why a âreturn to normalâ could mean disaster for the stock market - MarketWatch\",\"url\":\"https://www.marketwatch.com/story/why-a-return-to-normal-could-mean-disaster-for-the-stock-market-2020-04-22\",\"thumb\":\"https://s.marketwatch.com/public/resources/images/MW-IE973_corona_ZH_20200422114239.jpg\",\"author\":{\"karma\":3910,\"subscriptions\":[\"5e4dad2e5a6930001d9bad99\",\"5e9a298303c86d001c8cf584\",\"5e9aae5d03c86d001c8cf5b9\",\"5e9aa4d703c86d001c8cf5b4\",\"5e622896def3ed002821b9bf\",\"5e507210bdbc8100286fbeee\",\"5e4e3a4d5a6930001d9badb0\",\"5e9da81bd0ff32001c5fd0b9\",\"5e9eecf3ef8abb001ddc2696\"],\"username\":\"marcolanz\",\"created\":\"2020-03-23T03:27:46.936Z\",\"bitcoinAddress\":\"1EsBjmHakFkTnRxxcsB8M1hNHdUnyLg2w7\",\"links\":[],\"id\":\"5e782cb248f8c00027f14f10\"},\"category\":{\"subscriberCount\":2,\"nsfw\":false,\"_id\":\"5e9eecf3ef8abb001ddc2696\",\"name\":\"StockMarket\",\"description\":\"Stock market\",\"owner\":\"5e782cb248f8c00027f14f10\",\"created\":\"2020-04-21T12:54:11.398Z\",\"__v\":0},\"votes\":[{\"user\":\"5e782cb248f8c00027f14f10\",\"vote\":1},{\"user\":\"5e92618ba33715001c54530b\",\"vote\":1}],\"comments\":[],\"created\":\"2020-04-23T13:13:34.989Z\",\"text\":null,\"upvotePercentage\":100,\"commentCount\":0,\"id\":\"5ea1947ebbb15d001c408c4d\"}",
"querystring": "",
"headers": {
"User-Agent": "elasticsearch-js/7.6.1 (linux 4.15.0-96-generic-x64; Node.js v12.16.2)",
"Content-Type": "application/json",
"Content-Length": "1278"
},
"timeout": 30000
},
"options": {
"warnings": null
},
"id": 33
},
"name": "elasticsearch-js",
"connection": {
"url": "http://es01:9200/",
"id": "http://es01:9200/",
"headers": {},
"deadCount": 0,
"resurrectTimeout": 0,
"_openRequests": 1,
"status": "alive",
"roles": {
"master": true,
"data": true,
"ingest": true,
"ml": false
}
},
"attempts": 0,
"aborted": false
}
}
}
Here's the input:
{
score: 2,
views: 21,
type: 'link',
ranking: 2.7183885087703956,
sponsored: false,
_id: 5ea1947ebbb15d001c408c4d,
title: 'Why a âreturn to normalâ could mean disaster for the stock market - MarketWatch',
url: 'https://www.marketwatch.com/story/why-a-return-to-normal-could-mean-disaster-for-the-stock-market-2020-04-22',
thumb: 'https://s.marketwatch.com/public/resources/images/MW-IE973_corona_ZH_20200422114239.jpg',
author: {
karma: 3910,
subscriptions: [
5e4dad2e5a6930001d9bad99,
5e9a298303c86d001c8cf584,
5e9aae5d03c86d001c8cf5b9,
5e9aa4d703c86d001c8cf5b4,
5e622896def3ed002821b9bf,
5e507210bdbc8100286fbeee,
5e4e3a4d5a6930001d9badb0,
5e9da81bd0ff32001c5fd0b9,
5e9eecf3ef8abb001ddc2696
],
_id: 5e782cb248f8c00027f14f10,
username: 'marcolanz',
password: '$2a$10$qDEn46Qr0uGr0pGDP5FWO.aD411ARsOdVJpgzevWAgz1XR0tUW6kK',
created: 2020-03-23T03:27:46.936Z,
__v: 0,
ip: '201.21.39.32',
bitcoinAddress: '1EsBjmHakFkTnRxxcsB8M1hNHdUnyLg2w7',
links: []
},
category: {
subscriberCount: 2,
nsfw: false,
_id: 5e9eecf3ef8abb001ddc2696,
name: 'StockMarket',
description: 'Stock market',
owner: 5e782cb248f8c00027f14f10,
created: 2020-04-21T12:54:11.398Z,
__v: 0
},
votes: [
{ user: 5e782cb248f8c00027f14f10, vote: 1 },
{ user: 5e92618ba33715001c54530b, vote: 1 }
],
comments: [],
created: 2020-04-23T13:13:34.989Z,
text: null
}
await search
.update({
index: 'posts',
id: post._id,
body: { doc: post }, // this is the fix, wrap in "doc"
})
.catch(err => {
failed.updates += 1;
console.log('update failed', post._id);
});
With Elasticsearch 7.6 client, you need to wrap the body inside "doc" key. If you are using "script" along with "doc" then "doc" will be ignored.
Ref: https://www.elastic.co/guide/en/elasticsearch/reference/7.6/docs-update.html#_update_part_of_a_document
Related
so here is my stories model,
const storySchema = new mongoose.Schema(
{
avatar: { type: String, default: null },
handle: { type: String, default: null },
new: { type: Boolean, default: true },
isLive: {type: Boolean, default: false},
url: { type: String },
type: { type: String },
userName: { type: String, default: null },
userId: { type: mongoose.Schema.Types.ObjectId, ref: "user_details" },
isDeleted: { type: Boolean, default: false}
},
{
timestamps: true,
minimize: false
}
)
userId is refered to user_details, so currently when i list stories they get listed like this ,
one story at a time and sorted by userId and createdAt,
As you can see the first 2 stories has the same userId, and what i want to do is that i group the stories by the user Object.
"status": true,
"data": [
{
"_id": "633564ab793cf2a65f7c5dad",
"avatar": null,
"handle": null,
"new": true,
"isLive": false,
"url": "https://ellingsen-group.s3.amazonaws.com/media-1664443562856.png",
"type": "",
"userName": null,
"userId": "62eb5d58512ef25f1352830b",
"isDeleted": false,
"createdAt": "2022-09-29T09:26:03.846Z",
"updatedAt": "2022-09-29T09:26:03.846Z",
"__v": 0
},
{
"_id": "633564a9793cf2a65f7c5daa",
"avatar": null,
"handle": null,
"new": true,
"isLive": false,
"url": "https://ellingsen-group.s3.amazonaws.com/media-1664443559395.png",
"type": "",
"userName": null,
"userId": "62eb5d58512ef25f1352830b",
"isDeleted": false,
"createdAt": "2022-09-29T09:26:01.032Z",
"updatedAt": "2022-09-29T09:26:01.032Z",
"__v": 0
},
{
"_id": "633564e6793cf2a65f7c5dba",
"avatar": null,
"handle": null,
"new": true,
"isLive": false,
"url": "https://ellingsen-group.s3.amazonaws.com/media-1664443621607.png",
"type": "",
"userName": null,
"userId": "6290a0e7f03b0b3585e0f740",
"isDeleted": false,
"createdAt": "2022-09-29T09:27:02.608Z",
"updatedAt": "2022-09-29T09:27:02.608Z",
"__v": 0
},
{
"_id": "633564bf793cf2a65f7c5db0",
"avatar": null,
"handle": null,
"new": true,
"isLive": false,
"url": "https://ellingsen-group.s3.amazonaws.com/media-1664443582519.png",
"type": "",
"userName": null,
"userId": "6290a0e7f03b0b3585e0f740",
"isDeleted": false,
"createdAt": "2022-09-29T09:26:23.519Z",
"updatedAt": "2022-09-29T09:26:23.519Z",
"__v": 0
}
],
"totalPages": 1,
"message": "Get user story Feed Success"
want to change this, so for user 1 ( story 1, 2, 3) user 2 ( story 1,2 ) etc,
here is the query for the result above.
const stories: any = await Story.StoryModel.aggregate([{ $match: { '_id': { $in: combined } } }, { $sort: { userId: -1, createdAt: -1 } }, listStoriesFacetPagination]).exec()
I tried grouping them like this (below) but i get at error saying that stories.groupBy is not a function, I'm stuck at this point and been trying to work this out for the past week.
const groupByUserId = stories.groupBy((userId: any) => {
return story.userId;
});
and it would not work.
You can achieve this by using reduce method, the code will be like this:
const list = [{
'name': 'John',
'userId': '1',
},
{
'name': 'Anne',
'userId': '2',
},
{
'name': 'John',
'userId': '1',
},
{
'name': 'Anne',
'userId': '2',
},
];
const groups = list.reduce((groups, item) => ({
...groups,
[item.userId]: [...(groups[item.userId] || []), item]
}), {});
This code will result in a object like this:
{
"1": [
{'name': 'John', 'userId': '1'},
{'name': 'John', 'userId': '1'}
],
"2": [
{'name': 'Anne', 'userId': '2'},
{'name': 'Anne', 'userId': '2'}
]
}
Hope it help you :D
Here is the solution i found,
const stories: any = await Story.StoryModel.aggregate([
{ $match: { '_id': { $in: combined } } },
{ $group: {
_id: '$userId',
stories: { $push: { url: '$url', _id: '$_id' , isLive: '$isLive', avatar: '$avatar', type:'$type', handle:'$handle', new:'$new', userName:'$userName', userId:'$userId', isDeleted:'$isDeleted', createdAt:'$createdAt' } } } },
{ $sort: { createdAt: 1 } },
listStoriesFacetPagination]).exec()
I need to find the element by id, then go into the array of objects, find the desired by id and add the field guessId into it and insert the id key into it and assign the array of id. How can I do that ?
https://jsoneditoronline.org/?id=442f1dae0b2d4997ac69d44614e55aa6
In general, I need to create a GuessId field with such a structure
in fact, I am doing the voting, that is, the key in guessId will be go to vote, and the array to it, this is users who vote
{
"_id": "f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"bio": {
"firstname": "Лена",
"lastname": "фыв",
"middlename": "",
"company": "вв"
},
"files": [
{
"_id": "2e4e40c7-4df6-4974-8d16-bb24cd8134d6",
"destination": "./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"filename": "2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
"path": "uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
"folder": "f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"info": {
"size": 20805727,
"mimetype": "audio/mp3",
"encoding": "7bit",
"originalname": "Ахуевший Ленусик (Банк русский стандарт). Выпуск #5..mp3",
"fieldname": "selectedFile"
},
"userId": "5e05da745b21e61ccc84a892",
"date": "2019-12-27T10:19:12.213Z"
},
{
"_id": "81b94dea-ece6-421c-b68a-0aa59332cd0d",
"destination": "./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"filename": "81b94dea-ece6-421c-b68a-0aa59332cd0d.mp3",
"path": "uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\81b94dea-ece6-421c-b68a-0aa59332cd0d.mp3",
"folder": "f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"info": {
"size": 13515683,
"mimetype": "audio/mp3",
"encoding": "7bit",
"originalname": "Выпуск #75 Попрошайка НСВ..mp3",
"fieldname": "selectedFile"
},
"userId": "5e05da745b21e61ccc84a892",
"date": "2019-12-27T10:25:37.710Z"
}
],
"date": "2019-12-27T10:19:12.213Z",
"__v": 1
}
Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const HabalkaSchema = new Schema({
_id: {
type: String
},
bio: {
firstname: String,
lastname: String,
middlename: String,
company: String
},
files: [
{
_id: {
type: String
},
destination: {
type: String
},
filename: {
type: String
},
path: {
type: String
},
folder: {
type: String
},
info: {
size: {
type: Number
},
mimetype: {
type: String
},
encoding: {
type: String
},
originalname: {
type: String
},
fieldname: {
type: String
},
},
date: {
type: Date,
default: Date.now
},
bio: {
type: Object
},
userId: String,
guessId: {},
}
],
date: {
type: Date,
default: Date.now
}
});
module.exports = Habalka = mongoose.model('habalka', HabalkaSchema);
You can use the positional $ operator like this:
router.put("/habalka/:id/:fileId/:guessId", async (req, res) => {
const result = await Habalka.findOneAndUpdate(
{
_id: req.params.id,
"files._id": req.params.fileId
},
{
"files.$.guessId": {
[req.params.guessId]: ["a", "b"] //todo: get this array from req.body
}
},
{
new: true
}
);
res.send(result);
});
Route url: http://..../habalka/f58482b1-ae3a-4d8a-b53b-ede80fe1e225/2e4e40c7-4df6-4974-8d16-bb24cd8134d6/asda2
This will give you a result like this in the given file:
"files": [
{
"info": {
"size": 20805727,
"mimetype": "audio/mp3",
"encoding": "7bit",
"originalname": "Ахуевший Ленусик (Банк русский стандарт). Выпуск #5..mp3",
"fieldname": "selectedFile"
},
"date": "2019-12-27T10:19:12.213Z",
"_id": "2e4e40c7-4df6-4974-8d16-bb24cd8134d6",
"destination": "./uploads/f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"filename": "2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
"path": "uploads\\f58482b1-ae3a-4d8a-b53b-ede80fe1e225\\2e4e40c7-4df6-4974-8d16-bb24cd8134d6.mp3",
"folder": "f58482b1-ae3a-4d8a-b53b-ede80fe1e225",
"userId": "5e05da745b21e61ccc84a892",
"guessId": {
"asda2": [
"a",
"b"
]
}
},
And later if you want to add an item inside one of the guessId arrays, you can use the following code:
router.put("/habalka/:id/:fileId/:guessId", async (req, res) => {
const result = await Habalka.findOneAndUpdate(
{
_id: req.params.id,
"files._id": req.params.fileId
},
{ $push: { [`files.$.guessId.${req.params.guessId}`]: "c" } },
{
new: true
}
);
res.send(result);
});
I am ingesting various documents to Elastic Search like so:
await client.index({
id: url,
index: 'docs',
body: {
'Url': url,
'Accessed': new Date().toISOString(),
'Content': content,
'Title': title,
}
});
Now, I would like to perform a search on _source.Title only. This should be a fuzzy search on English text.
How do I achieve this using the Elastic Search library?
const result = await client.search({
index: 'docs',
from: 0,
size: 20,
body: {
"_source": [
"Url",
"Title",
"Accessed",
],
query: {
// What goes here?
}
},
});
I am using "#elastic/elasticsearch": "^7.4.0".
There are many ways to approach the problem. But initially I would use a match query with fuzziness:
const result = await client.search({
index: 'docs',
from: 0,
size: 20,
body: {
"_source": [
"Url",
"Title",
"Accessed",
],
query: {
"match" : {
"title" : {
"query" : "some title terms",
"fuzziness": 2
}
}
}
},
});
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-fuzzy-query.html
const result = await client.search({
index: 'docs',
from: 0,
size: 20,
body: {
"_source": [
"Url",
"Title",
"Accessed",
],
query: {
"fuzzy": {
"title": {
"value": "{searchQuery}"
}
}
}
},
});
I'm pretty new to Mongoose and MongoDB in general so I'm having a difficult time figuring out if something like this is possible:
I'm trying to filter only those document who has created_by_id not [].
This is schema.
var CampaignSchema = new Schema({
name: { type: String, required: true },
description: { type: String, required: true },
budget: { type: String, required: true },
tags: { type: [ String ], required: true },
status: { type: Number },
payment_id: { type: String },
created_by_id: [{ type: Schema.Types.ObjectId, ref: 'User' }],
attached_file: {
uploaded_on: { type: Date, default: Date.now },
uploaded_by: { type: String, required: true },
},
added_url: {
added_on: { type: Date, default: Date.now },
added_by: { type: String, required: true },
},
updated_by: { type: String },
created_on: { type: Date, default: Date.now },
updated_on: { type: Date }
});
This is code:
_getCampaigns(req, res){
var token = helpersMethods.getToken(req.headers);
var page = parseInt(req.query.page) || 0; //for next page pass 1 here
var limit = parseInt(req.query.limit) || 10;
var term = new RegExp(req.query.search, 'i');
var obj = { "created_by_id": { "$ne": [] } };
if (token) {
Campaign.find(obj)
.populate({
path : 'created_by_id',
match : {
$or: [
{ name: { $regex: term }},
]
}
})
.sort({ updateAt: -1 })
.skip(page * limit)
.limit(limit)
.exec((err, doc) => {
if (err) {
return res.json(err);
}
Campaign.count(obj).exec((count_error, count) => {
if (err) {
return res.json(count_error);
}
return res.json({
total: count,
page: page,
pageSize: doc.length,
campaigns: doc
});
});
});
} else {
return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
}
And i'm getting postman output like this, but i do not want the object who don't have created_by_id array:
{
"total": 2,
"page": 0,
"pageSize": 2,
"campaigns": [
{
"attached_file": {
"uploaded_by": "Demo user",
"uploaded_on": "2019-01-29T11:07:27.475Z"
},
"added_url": {
"added_by": "Demo user",
"added_on": "2019-01-29T11:07:27.475Z"
},
"tags": [
"tag1",
"tags2"
],
"_id": "5c5033ef28f63c72808f2225",
"created_by_id": {
"_id": "5c4965d477e7191c4d40b412",
"name": "Demo user",
"email": "demo#arth.tech",
"phone": "9918XXXXXX",
"type": "1",
"admin_rights": "1",
"password": "$2a$10$6T2ulNN60fBG9/vFgf8XhetkcWb/2zDxGXUMXMRi2Bltn8s1NEkbq",
"__v": 0,
"createdAt": "2019-01-24T07:31:03.327Z",
"loggedIn_at": "2019-01-30T06:33:04.388Z",
"loggedOut_at": "2019-01-24T08:03:44.091Z"
},
"name": "Test Campaign",
"description": "Discription of test campaign",
"budget": "2000",
"updated_by": "Demo User",
"created_on": "2019-01-29T11:07:27.475Z",
"__v": 0
},
{
"attached_file": {
"uploaded_by": "Demo User",
"uploaded_on": "2019-01-29T13:08:48.021Z"
},
"added_url": {
"added_by": "Demo user",
"added_on": "2019-01-29T13:08:48.021Z"
},
"tags": [
"test1",
"test2"
],
"_id": "5c505060b97f871123d97990",
"created_by_id": [],
"name": "Hello Campaign",
"description": "Description of Hello campaign",
"budget": "1000",
"updated_by": "Hello user",
"created_on": "2019-01-29T13:08:48.021Z",
"__v": 0
}
]
}
I want only those objects who has created_by_id, the actual output i want.
{
"total": 1,
"page": 0,
"pageSize": 1,
"campaigns": [
{
"attached_file": {
"uploaded_by": "Demo user",
"uploaded_on": "2019-01-29T11:07:27.475Z"
},
"added_url": {
"added_by": "Demo user",
"added_on": "2019-01-29T11:07:27.475Z"
},
"tags": [
"tag1",
"tags2"
],
"_id": "5c5033ef28f63c72808f2225",
"created_by_id": {
"_id": "5c4965d477e7191c4d40b412",
"name": "Demo user",
"email": "demo#arth.tech",
"phone": "9918XXXXXX",
"type": "1",
"admin_rights": "1",
"password": "$2a$10$6T2ulNN60fBG9/vFgf8XhetkcWb/2zDxGXUMXMRi2Bltn8s1NEkbq",
"__v": 0,
"createdAt": "2019-01-24T07:31:03.327Z",
"loggedIn_at": "2019-01-30T06:33:04.388Z",
"loggedOut_at": "2019-01-24T08:03:44.091Z"
},
"name": "Test Campaign",
"description": "Discription of test campaign",
"budget": "2000",
"updated_by": "Demo User",
"created_on": "2019-01-29T11:07:27.475Z",
"__v": 0
}
]
}
can any one help?
I did , what i want. here is updated code.
_getCampaigns(req, res){
var token = helpersMethods.getToken(req.headers);
var page = parseInt(req.query.page) || 0; //for next page pass 1 here
var limit = parseInt(req.query.limit) || 10;
var term = new RegExp(req.query.search, 'i');
var obj = {};
if (token) {
Campaign.find(obj)
.populate({
path : 'created_by_id',
match : {
$or: [
{ name: { $regex: term }},
]
}
})
.sort({ updateAt: -1 })
.skip(page * limit)
.limit(limit)
.exec((err, docs) => {
if (err) {
return res.json(err);
}else{
docs = docs.filter(function(doc) {
return doc.created_by_id.length != 0;
});
Campaign.count(obj).exec((count_error, count) => {
if (err) {
return res.json(count_error);
}else{
return res.json({
total: count,
page: page,
pageSize: docs.length,
campaigns: docs
});
}
});
}
});
} else {
return res.status(403).send({success: false, msg: 'Unauthorized.'});
}
}
I'm trying to get my head around this aggregation framework, and I'm kind of stuck, I would love some help.
So I have a collection of events, that simplified and already filtered look something similar to this:
{ creation: 54233, eventName: "elapsedTime", userId: 1, url: "/thing/53e245ca", data: { number: 5 } },
{ creation: 64033, eventName: "elapsedTime", userId: 1, url: "/thing/53e245ca", data: { number: 3 } },
{ creation: 50298, eventName: "elapsedTime", userId: 1, url: "/thing/53e245ca", data: { number: 2 } },
{ creation: 74220, eventName: "toggle", userId: 1, url: "/thing/53e245ca", data: { status: true } },
{ creation: 84233, eventName: "elapsedTime", userId: 2, url: "/thing/190345a9", data: { number: 8 } },
{ creation: 59511, eventName: "elapsedTime", userId: 2, url: "/thing/190345a9", data: { number: 10 } },
{ creation: 14236, eventName: "toggle", userId: 2, url: "/thing/190345a9", data: { status: false } },
{ creation: 80637, eventName: "toggle", userId: 2, url: "/thing/cc2b8b91", data: { status: true } },
{ creation: 99239, eventName: "toggle", userId: 2, url: "/thing/cc2b8b91", data: { status: false } }
And I want them to be aggregated like this:
{
userId: 1,
toggles: [
{ creation: 74220, url: "/thing/53e245ca", data: { status: true } }
],
elapsedSum: 10
},
{
userId: 2,
toggles: [
{ creation: 14236, url: "/thing/190345a9", data: { status: false } },
{ creation: 80637, url: "/thing/cc2b8b91", data: { status: true } },
{ creation: 99239, url: "/thing/cc2b8b91", data: { status: false } },
],
elapsedSum: 18
}
So I basically want to group by user, have the sum of all elapsedTime for each user, and have an array of all the toggle events for each user.
db.getCollection('heya').aggregate([
{
"$group": {
"_id": "$userId",
"toogles": {
"$addToSet": {
"$cond": {
"if": {
"$eq": [
"$eventName",
"toggle"
]
},
"then": {
"creation": "$creation",
"url": "$url",
"data": "$data"
},
"else": null
}
}
},
"elapsedSum": {
"$sum": {
"$cond": {
"if": {
"$eq": [
"$eventName",
"elapsedTime"
]
},
"then": "$data.number",
"else": 0
}
}
}
}
}
])
You may replace null from the array or unwind it to get the required result