While building Sequelize query unable to filter data from two tables with two different where conditions. Here I am adding some code...
Models:
//Task model
module.exports = function(sequelize, DataTypes) {
var Task = sequelize.define("Task", {
type: DataTypes.STRING(100),
start : DataTypes.DATE,
finish : DataTypes.DATE,
status : DataTypes.STRING(1),
}, {
classMethods: {
associate: function(models) {
Task.belongsTo(models.User, {
onDelete: "CASCADE",
foreignKey: {
allowNull: true
}
});
Task.hasOne(models.Evaluation);
Task.hasMany(models.Clarification);
}
}
});
return Task;
};
//Evaluation model
module.exports = function(sequelize, DataTypes) {
var Evaluation = sequelize.define("Evaluation", {
agent_id : DataTypes.INTEGER,
agent_name: DataTypes.STRING(100),
status: DataTypes.STRING(100),
}, {
classMethods: {
associate: function(models) {
Evaluation.belongsTo(models.Task, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
Evaluation.hasMany(models.Clarification);
}
}
});
return Evaluation;
};
//Clarification model
module.exports = function(sequelize, DataTypes) {
var Clarification = sequelize.define("Clarification", {
result : DataTypes.TEXT,
status : DataTypes.STRING(100),
comment : DataTypes.TEXT
}, {
classMethods: {
associate: function(models) {
Clarification.belongsTo(models.Task, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
Clarification.belongsTo(models.Evaluation, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
}
});
return Clarification;
};
I just added required code not exactly same.
Actually Using:
var filterobj = {
where: {
"UserId": { $ne: "NULL" }
},
include: [{
model: models.Evaluation,
attributes: ["id", "status","agent_name"],
where: {
"status": { $ne: "created" }
},
include: [{
model: models.EvalForm,
attributes: ["id", "name","LobId"],
include: [{
model: models.Lob,
attributes: ["id", "ClientId"]
},
{
model: models.FormDetails,
attributes: ["version", "status"]
}
]
}, {
model: models.Clarification,
attributes: ["id", "status"]
}],
required: true
},{
model: models.User,
attributes: ["name"]
}]
};
models.Task.findAll(filterobj).then(function (tasklist) {
// Doing some stuff for specific format
return res.send(tasklist);
}).catch(function (err) {
return res.send({ "error": { "code": 5000, "message": err.message } });
});
Response:
[
{
"id": 760,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-13T05:41:56.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 760,
"status": "completed",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": [
{
"id": 70,
"status": "raised"
},
{
"id": 71,
"status": "esclate"
}
]
},
"User": {
"name": "pooja.s"
}
},
{
"id": 761,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-12T14:35:30.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 761,
"status": "assigned",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": []
},
"User": {
"name": "pooja.s"
}
}
]
Expected Code(Not working)
var filterobj = {
where: {
"UserId": { $ne: "NULL" }
},
include: [{
model: models.Evaluation,
where: {
"status": { $in: ["assigned","completed"] }
},
attributes: ["id", "status","agent_name"],
include: [{
model: models.EvalForm,
attributes: ["id", "name","LobId"],
include: [{
model: models.Lob,
attributes: ["id", "ClientId"]
},
{
model: models.FormDetails,
attributes: ["version", "status"]
}
]
}, {
model: models.Clarification,
attributes: ["id", "status"]
}],
required: true
},{
model: models.User,
attributes: ["name"]
}, {
model: models.Clarification,
where: {
"status": { $in: ["raised"] }
}
attributes: ["id", "status"]
}]
};
models.Task.findAll(filterobj).then(function (tasklist) {
// Doing some stuff for specific format
return res.send(tasklist);
}).catch(function (err) {
return res.send({ "error": { "code": 5000, "message": err.message } });
});
Expected Response:
[
{
"id": 760,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-13T05:41:56.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 760,
"status": "completed",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": [
{
"id": 70,
"status": "raised"
},
{
"id": 71,
"status": "esclate"
}
]
},
"User": {
"name": "pooja.s"
},
"Clarifications": [
{
"id": 70,
"status": "raised"
}
]
},
{
"id": 761,
"type": "clarification",
"start": "2017-07-12T14:30:52.000Z",
"finish": "2017-07-12T14:35:30.000Z",
"status": "A",
"UserId": 854,
"Evaluation": {
"id": 761,
"status": "assigned",
"agent_name": "TestAgent1",
"EvalForm": {
"id": 5000008,
"name": "MobiPostpaid1",
"LobId": 26,
"Lob": {
"id": 26,
"ClientId": 1
},
"FormDetails": [
{
"version": 1,
"status": "A"
}
]
},
"Clarifications": []
},
"User": {
"name": "pooja.s"
},
"Clarifications": []
}
]
I can include Clarification table inside Task and Evaluation tables I have that association. The problem with where condition. It is pulling all the records. I am expecting data from Clarification table with status raised. And from Evaluation table status with assigned and completed. Some where I saw code using OR operator.
A.findAll({
where: {
$or: [
{'$B.userId$' : 100},
{'$C.userId$' : 100}
]
},
include: [{
model: B,
required: false
}, {
model: C,
required: false
}]
});
But this is not working for me.
Related
I aggregate users document:
User.aggregate([
{
$match: { _id: req.params.id },
},
{
$lookup: {
from: 'moods',
localField: '_id',
foreignField: 'source.userId',
pipeline: [
{
$lookup: {
from: 'contactrequests',
localField: 'source.userId',
foreignField: 'source.userId',
let: {
// truncate timestamp to start of day
moodsDate: {
$dateTrunc: {
date: '$timestamp',
unit: 'day',
},
},
},
pipeline: [
{
$match: {
$expr: {
$eq: [
'$$moodsDate',
{
// truncate timestamp to start of day
$dateTrunc: {
date: '$timestamp',
unit: 'day',
},
},
],
},
},
},
],
as: 'contactRequests',
},
},
],
as: 'calendar',
},
},
]).exec()
There is final document what I get
{
"_id": "P4SpYVd1KjBaF4SKyVw0E",
"lastName": "Doe",
"login": "User-01",
"name": "John"
"calendar": [
{
"_id": "62a351e33859aaf975c63323",
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
"deviceId": "Pacjent-141214"
},
"timestamp": "2022-06-07T12:44:13.333Z",
"mood": "good",
"contactRequests": []
},
{
"_id": "62a351f43859aaf975c63327",
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
"deviceId": "Pacjent-141214"
},
"timestamp": "2022-06-09T12:44:13.333Z",
"mood": "middle",
"contactRequests": [
{
"timestamp": "2022-06-09T12:44:13.333Z",
"source": {
"deviceId": "Pacjent-141214",
"userId": "P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": false,
"_id": "62a351ff3859aaf975c63329",
},
]
}
]
},
This is what I would to get. This is more clean and readable.
{
"_id": "P4SpYVd1KjBaF4SKyVw0E",
"login": "User-01",
"name": "John",
"lastName": "Doe",
"calendar": [
{
"timestamp": "2022-06-11T12:44:13.333Z"
"mood": {
"source": {
"userId": "P4SpYVd1KjBaF4SKyVw0E",
},
"timestamp": "2022-06-11T12:44:13.333Z",
"mood": "bad",
"_id": "62a352b83859aaf975c6332d",
},
"contactRequest": [
{
"timestamp": "2022-06-11T15:25:13.333Z",
"source" : {
"userId":"P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": true,
"_id": "62a351ff3859aaf975c63329"
},
{
"timestamp": "2022-06-11T18:23:13.333Z",
"source" : {
"userId":"P4SpYVd1KjBaF4SKyVw0E"
},
"resolve": false,
"_id": "62a351ff3859aaf975c63329"
},
]
}
}
]
}
To achive that I've used $group parameter, but at some point I have to declare which field should be fetch to result document and I have problem with contatRequest fields.
{
$group: {
_id: {
$dateToString: {
format: '%Y-%m-%d',
date: '$timestamp',
},
},
mood: {
$push: {
_id: '$_id',
source: '$source',
type: '$mood',
timestamp: '$timestamp',
},
},
contactRequest: {
$push: {
_id: '$contactRequest._id',
source: '$contactRequest.source',
resolve: '$contactRequest.resolve',
timestamp: '$contactRequest.timestamp',
},
},
},
},
{
$project: {
_id: 0,
timestamp: '$_id',
mood: 1,
contactRequest: 1,
},
},
Sample database/collections/aggregation pipeline at mongoplayground.net.
I tried to find users who are registered for that event.
So I join multiple collections shown below -
Events.aggregate([
{ $match: { category: "group_event" } },
// collection where events are scheduled
{
$lookup: {
from: "group_events",
let: { eventId: "$eventID" },
pipeline: [
{
$match: {
$expr: {
$and: [
{ $eq: ["$_id", "$$eventId"] },
{ $gt: ["$time", new Date()] },
],
},
},
},
// register user collection
{
$lookup: {
from: "register_events",
let: { eventId: "$_id" },
pipeline: [
{ $match: { $expr: { $eq: ["$eventId", "$$eventId"] } } },
],
as: "registerUsers",
},
},
],
as: "events",
},
},
{ $unwind: "$events" },
])
and the output is now comingout -
[
{
"_id": "614d6dfd82cb36be231083c9",
"trainerId": "61488dc36b7ccedbc884d20a",
"category": "group_event",
"eventID": "614d6dfc82cb36be231083c7",
"createdAt": "2021-09-24T06:19:41.268Z",
"updatedAt": "2021-09-24T06:19:41.268Z",
"__v": 0,
"events": {
"_id": "614d6dfc82cb36be231083c7",
"groupName": "group name 4",
"category": "sdfsdf",
"time": "2021-09-27T07:44:58.762Z",
"description": "description",
"day": "sunday",
"platform": "zoom",
"notes": "22",
"skills_to_learn": [
"demo"
],
"status": "pending",
"trainerId": "61488dc36b7ccedbc884d20a",
"meetingLink": "https://us05web.zoom.us/j/81660534858?pwd=cGZaODVjdWJUQWNtN243MlNiVUN0UT09",
"type": "group_event",
**isUserRegisted : true / false,**
"createdAt": "2021-09-24T06:19:41.000Z",
"updatedAt": "2021-09-24T06:19:41.000Z",
"__v": 0,
"registerUsers": [
{
"_id": "614ed6b4b8a545acb8517e85",
"userId": "614d59371d11becb8e23f536",
"eventId": "614d6dfc82cb36be231083c7",
"question": "",
"createdAt": "2021-09-25T07:58:44.939Z",
"updatedAt": "2021-09-25T07:58:44.939Z",
"__v": 0
}
]
}
}
]
which is ok for me bu just wanted to add a key: value, heighlited on obove section
isUserRegisted : true / false
i tried with $addFields but can't came up with any solution. Basically I need to retrieve arrays from registerUsers - collection and on the same time match the userId
I was able to figure out this issue.
simply I need to use $project and $filter to get the data if available and at last use $cond to return true or false
{
$project: {
root: "$$ROOT",
userFound: {
$filter: {
input: "$registerUsers",
as: "ac",
cond: {
$eq: ["$$ac.userId", mongoose.Types.ObjectId(userId)],
},
},
},
},
},
{
$project: {
_id: 0,
document: "$$ROOT",
userFound: {
$cond: {
if: { $isArray: "$userFound" },
then: {
$cond: {
if: {
$gt: [{ $size: "$userFound" }, 0],
},
then: true,
else: false,
},
},
else: false,
},
},
},
},
// merging nested object with parents
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
"$document.root",
{ isUserRegistered: "$userFound" },
],
},
},
},
I need help with the aggregate framework.
I have a model (currencies field can contain more than one object):
const schema = new mongoose.Schema({
country: { type: String },
code: { type: String },
region: [{
name: { type: String },
path: { type: Array },
city: [{
name: { type: String },
path: { type: Array },
latitude: { type: String },
longitude: { type: String },
}],
}],
currencies: [{
code: { type: String },
name: { type: String },
symbol: { type: String },
}],
})
And I need to receive all currencies without duplicates.
Received data can view like this:
[
{ code: 'string', name: 'sting', symbol: 'string' },
{ code: 'string', name: 'sting', symbol: 'string' },
...
]
// or like this:
[
currencies: [
{ code: 'string', name: 'sting', symbol: 'string' },
{ code: 'string', name: 'sting', symbol: 'string' },
...
]
]
I try to create a query
Geo.aggregate([
{
$group: {
_id: null,
currencies: { $addToSet: '$currencies' },
},
},
])
but receive this data with duplicates and it has many nested arrays:
[
{
"_id": null,
"currencies": [
[
{
"_id": "5cd9486248989616a411fac5",
"code": "JPY",
"name": "Japanese yen",
"symbol": "¥"
}
],
[
{
"_id": "5cd9491a48989616a411fb47",
"code": "TRY",
"name": "Turkish lira",
"symbol": null
}
],
I try this query:
Geo.aggregate([
{
$addFields: {
code: '$currencies.code',
name: '$currencies.name',
symbol: '$currencies.symbol',
},
},
])
But I receive error "TypeError: item is not iterable".
I need little help )
Db data views like this:
{
"_id": {
"$oid": "5c3334a8871695568817eadf"
},
"country": "Singapore",
"code": "sg",
"region": [
{
"path": [
"Singapore"
],
"_id": {
"$oid": "5c3366c63d92ac6e531e05c0"
},
"city": [],
"name": "Central Singapore Community Development Council"
},
....
],
"__v": 0,
"currencies": [
{
"_id": {
"$oid": "5cd948ec48989616a411fb28"
},
"code": "BND",
"name": "Brunei dollar",
"symbol": "$"
},
{
"_id": {
"$oid": "5cd948ec48989616a411fb27"
},
"code": "SGD",
"name": "Singapore dollar",
"symbol": "$"
}
]
}
In aggregate pipeline first you need to unwind the currencies array and then group them by condition to get desired result.
Geo.aggregate([
{
$unwind: '$currencies'
},
{
$group: {
_id: null,
currencies: { $addToSet: '$currencies' },
},
},
])
For more information you can look into documentation here
db.temp.aggregate([
{$project : {currencies : 1}},
{$unwind: "$currencies"},
{
$addFields: {
currencyHash: {
$concat : ['$currencies.code', "--", "$currencies.name", "--", "$currencies.symbol"]
}
}
},
{
$group: {
_id: "$currencyHash",
currency : {
$first : "$currencies"
}
}
},
{
$project: {
code : "$currency.code",
name : "$currency.name",
symbol : "$currency.symbol"
}
},
{
$project: {
_id : 0,
currency : 0
}
}
]).pretty()
I have been beating my head against this all day, and cannot seem figure out how to get this to work.
I have a source document like this:
{
"created_at": 1454700182,
"message_id": 160,
"user_id": 1,
"establishment_id": 1,
"geo": {
"coordinates": [-4.8767633,
89.7833547
],
"type": "Point"
},
"message": "Venus is in the west",
"active": true,
"score": 0,
"name": {
"first": "First",
"last": "Last"
},
"neighborhood": "Townside"
},
I create a document like this in ElasticSearch:
{
"message_id": 160,
"message": "Venus is in the west",
"first_name": "First",
"last_name": "Last",
"location": {
"lon": -4.8767633,
"lat": 89.7833547
},
"created_at": 1454700182,
"neighborhood": "Townside"
}
I've been trying different ways to create the index.
First:
client.indices.create({
index: 'messages',
type: 'document',
body: {
messages: {
properties: {
message: {
type: 'string',
index: 'not_analyzed'
},
neighborhood: {
type: 'string',
index: 'not_analyzed'
},
first_name: {
type: 'string',
index: 'not_analyzed'
},
last_name: {
type: 'string',
index: 'not_analyzed'
},
created_at: {
type: 'integer',
index: 'not_analyzed'
},
location: {
type: 'geo_point',
lat_lon: true
}
}
}
},
}
);
This allows me to do fuzzy text searches and greater than queries, but doesn't recognize the geo_point. So I tried this:
client.indices.create({
index: 'messages',
type: 'document',
"mappings": {
"messages": {
"properties": {
"message": {
"type": "string",
"index": "not_analyzed"
},
"neighborhood": {
"type": "string",
"index": "not_analyzed"
},
"first_name": {
"type": "string",
"index": "not_analyzed"
},
"last_name": {
"type": "string",
"index": "not_analyzed"
},
"created_at": {
"type": "integer",
"index": "not_analyzed"
},
"location": {
"type": "geo_point",
"lat_lon": true,
"index": "not_analyzed"
}
}
}
}
});
This does recognize the geo_point, but none of the other things work.
Here is the query I've been using for the non geo fields:
query = {
query: {
filtered: {
query: {
multi_match: {
query: message,
fields: ['message', 'neighborhood', 'first_name', 'last_name'],
"fuzziness": "AUTO",
"prefix_length": 2
}
},
filter: {
bool: {
must: {
range: {
"created_at": {
"gte": min_ts
}
}
}
}
}
}
}
};
I've been so turned around on this, just trying to allow text and geo search on the same collection of documents, that I need at least another set of eyes.
Appreciate any help!
How can i bind my child data in one column?
I want to write "Technology,Economy,Life" in same column and same row. But i think i need to loop in "Category". How can i do this, any idea?
My Data:
{
"ParentId": "00000000-0000-0000-0000-000000000000",
"Title": null,
"UserGroupModel": null,
"EntityAccessData": [
{
"EntityTitle": "User",
"Access": {
"Id": "59d0c6f7-8f93-497a-854d-bdd4a42ade94",
"Title": "Can Delete"
},
"Category": [
{
"Id": "00000000-0000-0000-0000-000000000000",
"Title": "Technology"
},
{
"Id": "00000000-0000-0000-0000-000000000000",
"Title": "Economy"
},
{
"Id": "00000000-0000-0000-0000-000000000000",
"Title": "Life"
}
],
"HasAccess": true
},
{
"EntityTitle": "UserGroup",
"Access": {
"Id": "7c65be44-11b0-4cf4-9104-0ad999e7e280",
"Title": "Can Edit"
},
"Category": [
{
"Id": "00000000-0000-0000-0000-000000000000",
"Title": "Technology"
},
{
"Id": "00000000-0000-0000-0000-000000000000",
"Title": "Economy"
},
{
"Id": "00000000-0000-0000-0000-000000000000",
"Title": "Life"
}
],
"HasAccess": true
}
]
}
My Script:
$("#grid").kendoGrid({
dataSource: {
type: "json",
transport: {
read: "/getData" },
schema: {
data: "EntityAccessData"
},
group: [{
field: "EntityTitle"
}],
},
columns: [
{
field: "Access.Id",
title: "ID"
},
{
field: "Access.Title",
title: "Access title"
},
{
field: "HasAccess",
title: "has access"
},
{
field: "Category.Title", // ***wrong***
title: "Category"
},
]
});
Define the schema as follow:
schema: {
data : "EntityAccessData",
model: {
CategoryTitle: function () {
var category = [];
$.each(this.Category, function(idx, elem) {
category.push(elem.Title);
});
return category.join(",");
}
}
},
Where I add an additional field CategoryTitle that is the result of joining the Title of the Category array and then define the column as:
{
field: "CategoryTitle()",
title: "Category"
}