What is wrong with this sequelize query - node.js

I'm biuld an Webapp with Sequelize ORM. My query:
if(req.query.dataTransIni && req.query.dataTransFim ){
condition.data = {
[op.or] : {
[op.between] : [
{logdatatransacao : moment(req.query.dataTransIni).format("YYYY-MM-DD")},
{logdatatransacao : moment(req.query.dataTransFim).format("YYYY-MM-DD")}
],
[op.between] : [
{arqdataTransacao : moment(req.query.dataTransIni).format("YYYY-MM-DD")},
{arqdataTransacao : moment(req.query.dataTransFim).format("YYYY-MM-DD")}
]
}
}
}
The error in console
UnhandledPromiseRejectionWarning: Error: Invalid value { arqdataTransacao: '2019-10-10' }
What I want looks like it
select * from myTable
where
logdatatransacao between ('2020-02-27') and ('2020-02-28')
or
arqdataTransacao between ('2020-02-27') and ('2020-02-28')

I see you are specifying the column names within an object again in the between Op.
The following works for me by supplying an scalar array:
const startDate = moment('2020-01-01').format("YYYY-MM-DD");
const endDate = moment('2020-01-02').format("YYYY-MM-DD");
const departments = await sequelize.models.department.findAll({
where: {
created: {
[Sequelize.Op.between]: [startDate, endDate]
}
},
attributes: ['name'],
raw: true,
nest: true
});
SQL:
SELECT "name" FROM "departments" AS "department" WHERE "department"."created" BETWEEN '2020-01-01 08:00:00.000 +00:00' AND '2020-01-02 08:00:00.000 +00:00';

Solved my problem doing this:
var condition = [];
...
if(req.query.dataTransIni && req.query.dataTransFim ){
condition.push({
[op.or] : [{
logdatatransacao : {
[op.between] : [
moment(new Date(req.query.dataTransIni)).format("YYYY-MM-DD"),
moment(new Date(req.query.dataTransFim)).format("YYYY-MM-DD")
]
}
},
{
arqdataTransacao : {
[op.between] : [
moment(new Date(req.query.dataTransIni)).format("YYYY-MM-DD"),
moment(new Date(req.query.dataTransFim)).format("YYYY-MM-DD")
]
}
}]
})
}
...
const Vscmc = await Modelvscmc.findAll({
offset : ((offsetPage-1)*limit),
limit: limit,
where : condition
})
My problem was Brackets and Parentheses objects.

Related

writing filter in loopback

I'm trying to write a loopback 4 filter that returns the objects where creatorId = userId OR userId in sharedUsers = userId but I cant seem to formulate the filter correctly
(neither of these properties are unique id's)
this is what my object shape looks like:
{
"_id" : "20",
"configMetadata" : {
...
"creatorId" : "50",
"creatorName" : "Mark"
},
"sharedUsers" : [
{
"userId" : "15"
},
{
"userId" : "20"
}
],
"sharedRoles" : ....,
"tiles" : ...
}
here is what I tried
const filter2: Filter<ProductViewConfig> =
{
where: {
or: [
{ configMetadata : { creatorId : userId} },
{ sharedUsers: { [userId]: userId } },
],
},
};
but I'm getting 2 errors: one is : configMetadata is missing the rest of the properties, so just properties mismatch and for sharedUsers I'm getting a MongoError: unknown operator: $15 when I enter userId = 15
to provide context: this is what my endpoint looks like
#get('/product-view-configs/{userId}')
#response(200, {
description: 'ProductViewConfig model instance',
content: {
'application/json': {
schema: getModelSchemaRef(ProductViewConfig),
},
},
})
async findIt(
#param.path.string('userId') userId: string,
#param.filter(ProductViewConfig, { exclude: 'where' }) filter?: FilterExcludingWhere<ProductViewConfig>
): Promise<ProductViewConfig> {
//returns configs where creatorId = userId OR userId in sharedUsers = userId
const filter2: Filter<ProductViewConfig> =
{
where: {
or: [
{ configMetadata : { creatorId : userId} },
{ sharedUsers: { [userId]: userId } },
],
},
};
const records = this.productViewConfigRepository.find(filter2);
return this.productViewConfigRepository.findById(userId, filter);
}
can anyone point me in the right direction? I couldn't find examples on how to filter nested objects in the documentation, so any help would be appreciated!

MongoDB update multiple items in an array of objects with corresponding data

I have an array in my MongoDB document as shown below:
{
...otherDocFields,
groupID: "group-id",
users: [
{id: "uid1", name: "User1"},
{id: "uid2", name: "User2"},
{id: "uid3", name: "User3"},
{id: "uid4", name: "User4"}
]
}
I'm trying to write a function that will update users' names based on that ID.
I tried something like:
async function updateNames(groupID: string, data: Array<{id: string, name: string}>) {
try {
// MongoDB Aggregation
await mongoDB.collection("users").aggregate([
{$match: {groupID}},
{$unwind: {
path: '$users',
includeArrayIndex: 'users.id',
preserveNullAndEmptyArrays: true
}
}
//....
])
} catch (e) {
console.log(e)
}
}
I'm stuck at the part to update the relevant names from the data param in the function.
A sample data would be:
[
{id: "uid1", name: "newName1"},
{id: "uid3", name: "newName3"}
]
I can for sure read, manually process it and update the document but I'm looking for a way to do it in single go using aggregation.
You can do this with an update statement using array filters (https://docs.mongodb.com/manual/reference/operator/update/positional-filtered/#---identifier--)
First, if we just declare some tests data which would be passed into your method:
const data = [
{id: "uid2", name: "ChangeName1"},
{id: "uid4", name: "ChangedName2"}
];
We can then create an update statement which updates all the users within that list within a map and a reduce:
const sets = data.map((element, index) => ({ [`users.$[f${index}].name`]: element.name })).reduce((accumulator, currentValue) => (Object.assign(currentValue, accumulator)), { });
const update = { $set: sets };
This will give us the following update statement:
{
"$set" : {
"users.$[f1].name" : "ChangedName2",
"users.$[f0].name" : "ChangeName1"
}
}
We can create a bunch of array filters with that data:
const arrayFilters = data.map((element, index) => ({ [`f${index}.id`]: element.id }));
This will give us the following which we can pass into the options of an update.
[ { "f0.id" : "uid2" }, { "f1.id" : "uid4" } ]
Last of all we can execute the following update command:
db.users.updateOne(
filter,
update,
{ arrayFilters }
);
Now if we check the output we'll get the following results
> db.users.find().pretty()
{
"_id" : ObjectId("60e20841351156603932c526"),
"groupID" : "123",
"users" : [
{
"id" : "uid1",
"name" : "User1"
},
{
"id" : "uid2",
"name" : "ChangeName1"
},
{
"id" : "uid3",
"name" : "User3"
},
{
"id" : "uid4",
"name" : "ChangedName2"
}
]
}

Issue with sequelize nested query - Sub query returns array object

const pEntries = await DB.models.pcPhoto.findAll({
raw : true,
attributes : ['filename'],
where: {
eid : { $in: await DB.models.entrySettings.findAll({
attributes : ['id'],
raw: true,
where: {
emplId : '99999'
}
})
}
}
});
I get the following result from the subquery
[ {id: 801},{id: 802},.....{id:900} ]
While executing the query the error is
UnhandledPromiseRejectionWarning: Error: Invalid value { id: 968 }
How to handle this and get the only array of values so that the query is executed smoothly.
const settings = await DB.models.entrySettings.findAll({
attributes: ['id'],
raw: true,
where: { emplId : '99999' }
})
const eIds = settings.map(el => el.id)
const pEntries = await DB.models.pcPhoto.findAll({
raw : true,
attributes : ['filename'],
where: {eid: {
$in: eIds
}}
}
You can do that in single query , without using sub-query , but for that you need to define proper association b/w model like , and that's a good way to go
pcPhoto.hasMany(entrySettings , { foreignKey: 'eid' });
And then just run :
DB.models.pcPhoto.findAll({
raw : true,
attributes : [ 'id' , 'filename' , 'eid' ],
include : {
model : DB.models.entrySettings ,
where : { emplId : '99999' }
}
});

Use variable in mongodb object dot notation

I want to increment a property value of an object if it does exist inside an array.
Mongo record:
{
"_id" : ObjectId("5b7bdd9f0465e8345ba83aad"),
"userID" : "400",
"userName" : "Jon Snow",
"pageName" : "1",
"courseName" : "Maths",
"socketID" : [
"aswKWYyE1euk2GNIAAAD"
],
"online" : true,
"userHistory" : {
"pagesVisited" : [
{
"page" : "1",
"timesVisited" : 1
}
],
"coursesVisited" : [
"Maths"
]
},
"date" : ISODate("2018-08-21T09:38:39.281Z")
}
Here on userHistory.pagesVisited on the page property if I get the value 1 again then I want to increment the timesVisited property like so:
"pagesVisited" : [
{
"page" : "1",
"timesVisited" : 2
}
],
Here's what I have tried with no luck:
let userDetails = {
userID: queryUser.userID,
userName: queryUser.username,
pageName: queryUser.pageName,
courseName: queryUser.courseName,
socketID: [socket.id],
online: true,
userHistory: {
pagesVisited: [
{
"page" : queryUser.pageName,
"timesVisited" : 1
}
],
coursesVisited: [queryUser.courseName]
},
date: new Date()
};
onlineUsersDB.findOne({'userID': userDetails.userID}).then(async (user) => {
if (user) {
let page = {"page": queryUser.pageName, "timesVisited": 1};
let course = queryUser.courseName;
let updatedUser = await onlineUsersDB.findOneAndUpdate(
{'userID': user.userID},
{
$set: {'online': true},
$push: { 'socketID': socket.id },
},
{ $addToSet: { 'userHistory.coursesVisited': course } },
{ returnOriginal: false }
);
let updatedUserPageRef = updatedUser.value.userHistory.pagesVisited;
if (updatedUserPageRef) {
let pageFound = await updatedUserPageRef.findIndex(item => item.page === page);
if (pageFound >= 0) {
let updatedUserPage = await onlineUsersDB.findOneAndUpdate(
{'userID': updatedUser.value.userID},
// Here I want to reference the variable pageFound
{$inc: { 'userHistory.pagesVisited.[pageFound].timesVisited': 1 }},
{ returnOriginal: false }
);
console.log(JSON.stringify(updatedUserPage,null, 2));
}
}
let users = await onlineUsersDB.find({'online': true}).toArray();
io.to(room).emit('online-users', users);
io.to(room).emit('user-back-online', updatedUser);
} else {
if (userDetails.userID !== '100') {
await onlineUsersDB.insert(userDetails);
}
let users = await onlineUsersDB.find({'online': true}).toArray();
io.to(room).emit('online-users', users);
}
}).catch((e) => console.log(e));
In the above code where my comment is I want to reference the variable pageFound in my object dot notation like so:
{$inc: { 'userHistory.pagesVisited.[pageFound].timesVisited': 1 }}
It works when I give it a hardcoded value like:
{$inc: { 'userHistory.pagesVisited.0.timesVisited': 1 }}
After experimenting a little bit I made it to work like this:
I broke out my query string into another variable using template literals.
let pageInc = `userHistory.pagesVisited.${pageFound}.timesVisited`;
And then referenced the variable in my query like so:
{$inc: { [pageInc]: 1 }}
And it works now.

Mongo + check multiple fields existing

I am working mongo with nodejs.
I have array list:
var checkFields = ["field1","field2","field3"];
I try to get the count of records having the array list fields and user field is equal to admin.
Sample data:
[
{
"checkFields": {
"field1": "00124b3a5c31",
"user": "admin"
}
},
{
"checkFields": {
"field2": "00124b3a5c31",
"user": "admin"
}
},
{
"checkFields": {
"field1": "00124b3a5c31",
"user": "regular"
}
}
]
Query:
db.collection_name.find(
{"checkFields.user" : "admin"}
{ "checkFields.field1": { $exists: true} }
)
Expected Result:
Result is to get rows of count of matching the field in array list(checkFields).
Building up an $or array for the list of field existence checks is the right approach, but assuming you're on a current node.js build you can simplify the query creation to:
var checkFieldsLists = checkFields.map(field => ({
['checkFields.' + field]: {$exists: true}
}));
var query = {
$or: checkFieldsLists,
'checkFields.user': 'admin'
}
This removes the superfluous $or for the "user is admin" check which lets you also remove the outer $and, so that the generated query is:
{ '$or':
[ { 'checkFields.field1': { '$exists': true } },
{ 'checkFields.field2': { '$exists': true } },
{ 'checkFields.field3': { '$exists': true } } ],
'checkFields.user': 'admin' }
I tried the following code. Its working but don't know its good solution and perfomance. Please anyone have better answer means please post it.
var checkFields = ["field1", "field2", "field3"];
var checkFieldsLists = [];
for ( i = 0; i < checkFields.length; i++) {
var jsObj = {};
jsObj['checkFields.' + checkFields[i]] = {};
jsObj['checkFields.' + checkFields[i]].$exists = true;
checkFieldsLists.push(jsObj);
}
var query = {
"$and" : [{
"$or" : checkFieldsLists
}, {
"$or" : [{
"checkFields.user" : "admin"
}]
}]
};
console.log(JSON.stringify(query));
//console log will return
/*
{"$and":[{
"$or" : [{
"checkFields.field1" : {
"$exists" : true
}
}, {
"checkFields.field2" : {
"$exists" : true
}
}, {
"checkFields.field3" : {
"$exists" : true
}
}]
}, {
"$or" : [{
"checkFields.user" : "admin"
}]
}]
}
*/
collection.find(query);
Here is the solution using aggregate query.
var Db = require('mongodb').Db, Server = require('mongodb').Server, assert = require('assert');
var db = new Db('localhost', new Server('localhost', 27017));
var checkFields = ["field1", "field2", "field3"];
var checkFieldsLists = [];
for (var i = 0; i < checkFields.length; i++) {
var jsObj = {};
jsObj['checkFields.' + checkFields[i]] = {};
jsObj['checkFields.' + checkFields[i]].$exists = true;
checkFieldsLists.push(jsObj);
}
var query = {
"$and" : [{
"$or" : checkFieldsLists
}, {
"$or" : [{
"checkFields.user" : "admin"
}]
}]
};
var matchQuery = {
"$match" : {
"checkFields.user" : "admin",
"$or" : checkFieldsLists
}
};
var groupQuery = {
$group : {
_id : null,
count : {
$sum : 1
}
}
};
var aggregateCheckFields = function(db, callback) {
console.log("Match query is ====>" + JSON.stringify(matchQuery));
console.log("Group query is ====>" + JSON.stringify(matchQuery));
db.collection('checkfields').aggregate([ matchQuery, groupQuery ]).toArray(
function(err, result) {
assert.equal(err, null);
console.log("Result is ===>" + JSON.stringify(result));
if (result.length > 0) {
console.log("Count is ===>" + result[0].count);
}
callback(result);
});
};
db.open(function(err, db) {
aggregateCheckFields(db, function() {
db.close();
});
});
Output:-
Result is ===>[{"_id":null,"count":3}]
Count is ===>3

Resources