I'm using loopback models to filter a list of events with start and end dates and I want to return a list of events that are NOT between two specific dates. I thought something like this should work:
const eventList = await Events.find({
where: {
startDate: {
not: {
between: [unavailableStarting, unavailableEnding],
},
},
endDate: {
not: {
between: [unavailableStarting, unavailableEnding],
},
},
},
});
If you take out the not part, this works fine for getting all events that ARE between the dates. How do I make a NOT BETWEEN filter work?
As far as I can tell there is no way to use NOT BETWEEN with loopback models, but I did make it work with lt gt:
const eventList = await Events.find({
where: {
and:[{
or: [
{ startDate: { lt: unavailableStarting } },
{ startDate: { gt: unavailableEnding } },
]
},
{
or: [
{ endDate: { gt: unavailableStarting } },
{ endDate: { lt: unavailableEnding } },
]
}
]},
});
Related
I am making a search in a database which have a table productDetails.
I want a query something like this in sequelize,
SELECT * FROM productDetails WHERE title LIKE '%search1%' OR title LIKE '%search2%'
currently using
const pd= await ProductDetail.findAll({
where: {
[Op.or]: {
title: { [Op.like]: `%search1%` },
title: { [Op.like]: `%search2%` },
},
},
Just indicate all conditions as an array as a value of Op.or like this:
where: {
[Op.or]: [
{ title: { [Op.like]: `%search1%` } },
{ title: { [Op.like]: `%search2%` } },
],
},
See examples here
I am trying to use Prisma to return a count for a boolean field where it equals 'true'.
To give some context, on the frontend I am trying to calculate the workouts that have been completed by a user as a percentage, so ideally I would like prisma to return a count for the total workouts (which I have successfully done) and the count for the userWorkouts where 'isCompleted' equal true (which I am unable to achieve), currently the count is returning all userWorkouts not just the completed ones.
Here is my current Prisma Query:
const response = await prisma.user.findUnique({
where: {
id: 1,
},
select: {
id: true,
programs: {
select: {
program: {
select: {
name: true,
blocks: {
select: {
id: true,
name: true,
week: {
select: {
id: true,
number: true,
workouts: {
select: {
userWorkouts: {
where: {
isCompleted: true,
},
},
_count: {
select: {
userWorkouts: true,
},
},
},
},
_count: {
select: {
workouts: true,
},
},
},
},
},
},
},
},
},
},
},
});
res.json(response);
};
Is this possible to achieve using Primsa? Or should I just return all userWorkouts and filter for isCompleted: true on the frontend?
I have spoken with the Prisma team and this can't be achieved yet, although there is a feature request open for it.
https://github.com/prisma/prisma/issues/8413
If you would like to help get this feature added please add you +1 to the feature request.
I have this JSON FILE
{
"_id": "GgCRguT8Ky8e4zxqF",
"services": {
"emails": [
{
"address": "Abunae#naa.com",
"verified": false,
"verifiedMail": "Toto#hotmail.com"
}
],
"profile": {
"name": "Janis"
},
"pushIds": []
}
I want to update my verifiedMail field but couldn't figure out how to do it in Meteor, it's always returning me an error
let VerifiedEmail = "Exemple1"
await Meteor.users.update({ _id: user._id }, { $set: { 'emails.verifiedEmail': emailRefactor} }, { upsert: true })
Couldn't figure out how to access the emails.verifiedEmail field
Tried this exemlpe worked like a charm
let VerifiedEmail = "Exemple1"
await Meteor.users.update({ _id: user._id }, { $set: { 'profile.name': emailRefactor} }, { upsert: true })
but couldn't figure out how to access emails.verifiedEmail .
Could you please help me ?
Emails is an array, while profile is an object. You have to access the first object of the email array instead
This updates the exact email address from emails
Meteor.users.update({
"emails.address": emailRefactor
}, {
$set: {
"emails.$.verified": true
}
});
Or update the first element
Meteor.users.update({
_id: user._id,
"emails.address": emailRefactor
}, {
$set: {
"emails.0.verified": true
}
});
You're trying to set verifiedEmail while the actual field is verifiedMail.
I am using mongoDb, restheart and also new in these technologies. I want to add one more field in aggregation. So my exist aggregation working fine. I am sharing aggregation
Aggregation:
aggrs: [{
type: "pipeline",
uri: "by_time",
stages: [{
_$match: {
bus::destination: {
_$in: {
_$var: "stands"
}
},
bus::eta: {
_$gte: {
_$var: "dateFrom"
},
_$lte: {
_$var: "dateTo"
}
}
}
},
]
}]
Json Data:
{
_id: "1938378_32323",
bus: {
valid: true,
eta: {
$date: 1500661200000
},
busDate: {
$date: 1500595200000
},
etd: {
$date: 1500661200000
},
origin: "kalu",
destination: "cell",
},
apose: false
}
Above aggregation working fine, But I want to add "apose" in aggregation. So I have implemented this code for aggregation.
stages: [{
_$match: {
bus::destination: {
_$in: {
_$var: "stands"
}
},
bus::eta: {
_$gte: {
_$var: "dateFrom"
},
_$lte: {
_$var: "dateTo"
}
},
"apose": {
"_$eq": {
"_$var": "opposit"
}
}
}
},
]
But When I hit this aggregation, I got only
{"_embedded":[],"_size":0,"_total_pages":0,"_returned":0}
My hitting url is
http:...abc.../_aggrs/by_time?avars={"stands":["KALU","MAL","SALU"],"dateFrom":{"$date":"2007-12-21T01:30:00Z"},"dateTo":{"$date":"2010-02-15T09:30:00Z"},"opposit":"false"}
your answer is valuable for me. Thanks
I have documents of the form
challenge:
{
"name": "challenge by abdul",
"created_user_id": "1",
"game_id": "123",
"platform_id": "9857",
"amount": 30
}
game:
{
"_id": "auto_generated",
"name": "NFS",
"version": "3",
}
platform:
{
"_id": "auto_generated",
"name": "ps2",
"version": "sami"
}
I want to perform join query in sails and want result in below format
{
"name": "challenge by abdul",
"created_user_id": "1",
"game_id": "123",
"game_name":"NFS",
"platform_name": "ps2",
"platform_id": "9857",
"amount": 30
}
There is no join in Sails but populate. So you need make associations between models and populate them. Example:
// api/models/Platform.js
module.exports = {
attributes: {
name: {
type: 'string'
},
version: {
type: 'string'
},
game: {
model: 'Game',
via: 'platform'
}
}
};
// api/models/Game.js
module.exports = {
attributes: {
name: {
type: 'string'
},
version: {
type: 'string'
},
platform: {
model: 'Platform',
via: 'game'
}
}
};
You can write following code then:
// api/controllers/AnyController.js
module.exports = {
index: function(req, res) {
Game
.findOne({name: 'MY_GAME'})
.populate('platform')
.then(function(game) {
console.log(game); // Game model
console.log(game.platform); // Platform model
return game;
})
.then(res.ok)
.catch(res.negotiate);
}
};