Variable into mongodb query - javascript

ok this is driving me crazy..
function(amount){
matchingOrder = Order.findOne({
Price: {
$lte: amount
}
}, {
sort: {
Price: 1,
Order_ID: 1
}
});
}
-----does not work
this works:
function(){
amount = 2
matchingOrder = Order.findOne({
Price: {
$lte: amount
}
}, {
sort: {
Price: 1,
Order_ID: 1
}
});
}
in both cases console.log(amount) is 2 so variable gets passed
...sorry for maybe obvious scope or something..im relativly new at this

The only thing coming to my mind could be different types for amount. Try this instead:
function(amount){
matchingOrder = Order.findOne({
Price: {
$lte: parseInt(amount)
}
}, {
sort: {
Price: 1,
Order_ID: 1
}
});
}

function query_am(input){
var amount = input; //pay attention, as pre-condition your input must be a number.
matchingOrder = Order.findOne({
Price: {
$lte: amount
}
}, {
sort: {
Price: 1,
Order_ID: 1
}
});
}
To invoke this function, for example: query_am(2) or query_am(6) . don't do query_am('6')

Related

Error: Arguments must be aggregate pipeline operators using $lt

filteredListings = await Listing.aggregate([
{ $match: { category: req.params.category } },
{ price: { $lt: 150 } }
]);
As you can see, I'm trying to get all listings with a field price of less than 150. what is the correct way of doing that?
You need to add it in $and operator inside the $match statement
filteredListings = await Listing.aggregate([
{ $match: { $and: [{ category: req.params.category }, { price: { $lt: 150 } }] } }
]);

Meteor Collection Hooks - How to run the 'after insert' hook only once the previous 'after insert' has finished

Inserts are happening quicker than the after.insert function can process them and what I want to happen after the current document is inserted is dependent on what happened after the previous document was inserted.
I think the short question might be how do I make it synchronous?
FirstCollection.after.insert(function (userId, doc) {
lastDoc = SecondCollection.findOne({}, { sort: { $natural: -1 } });
if (!lastDoc) {
SecondCollection.insert({
startNumber: doc.txNumber,
count: 1,
duration: 264,
lastTx: doc.txNumber,
time: Date.now(),
finishedAt: 0,
});
return;
}
if (lastDoc.finishedAt !== 0) {
if (Date.now() - lastDoc.finishedAt < 60000) {
return;
}
SecondCollection.insert({
startNumber: doc.txNumber,
count: 1,
duration: 264,
last: doc.txNumber,
time: Date.now(),
finishedAt: 0,
});
return;
}
SecondCollection.update(
{ _id: lastDoc._id },
{ $set: { lastTx: doc.txNumber }, $inc: { count: 1 } }
);
if (lastDoc.count === lastDoc.duration) {
SecondCollection.update(
{ _id: lastDoc._id },
{ $set: { finishedAt: Date.now() } }
);
}
});

Add aggregation by the condition of the value of your own variable

I have two functions to get an array from monga using aggregation. The functions are completely the same, except for one pipeline - "match ({startTime: {$ gte: start}})". How can I leave one function and add a "math" only by the presence of the "start" variable, which is a date filter?
let groupedByUserSessions
if (lastDay) {
const start = getDateFromString(lastDay);
groupedByUserSessions = await getValuesByDate(start)
} else {
groupedByUserSessions = await getAllValues();
}
The functions are completely the same,
function getValuesByDate(start) {
return Sessions.aggregate()
.match({ startTime: { $gte: start } })
.group({
_id: { departament: "$departament", userAdName: "$userAdName" },
cleanTime: { $sum: { $subtract: ["$commonTime", "$idlingTime"] } }
})
.group({
_id: { departament: "$_id.departament"},
users: { $push: {value: '$cleanTime', name: '$_id.userAdName'} },
commonCleanTime: { $sum: "$cleanTime" }
})
.project({
departament: '$_id.departament',
users: '$users',
commonCleanTime: '$commonCleanTime',
performance: { $divide: [ "$commonCleanTime", { $size: "$users" }] }
});
}
You can break the pipeline into a "head" and a "tail" and construct the head differently when the start argument is given:
function getValuesByDate(start) {
let agg = Sessions.aggregate();
if (start) {
agg = agg.match({ startTime: { $gte: start } });
}
return agg
.group({
_id: { departament: '$departament', userAdName: '$userAdName' },
cleanTime: { $sum: { $subtract: ['$commonTime', '$idlingTime'] } },
})
.group({
_id: { departament: '$_id.departament' },
users: { $push: { value: '$cleanTime', name: '$_id.userAdName' } },
commonCleanTime: { $sum: '$cleanTime' },
})
.project({
departament: '$_id.departament',
users: '$users',
commonCleanTime: '$commonCleanTime',
performance: { $divide: ['$commonCleanTime', { $size: '$users' }] },
});
}

mongodb add item in array

await Cart.update(
{ $elemMatch: { user_id: decoded._id } },
{
$addToSet: {
"cart.&.items": {
product_id: req.query.product_id,
quantity: 1,
},
},
}
);
My goal is to add elements to the array of items in the cart.
There's no syntax with & sign. MongoDB offers $ as a positional operator which allows you to modify existing item but you don't needed since you just want to append a new object to an array, try:
{
$addToSet: {
"cart.items": {
product_id: req.query.product_id,
quantity: 1,
}
}
}
Cart.updateMany(
{ user_id: decoded._id },
{
$push: {
"cart.items": {
product_id: req.query.product_id,
quantity: 1,
},
},
}
)
I finally found the right method through trial and error.

Access object in object for the next date

I know this has been asked before but I can't seem to find the answer, how to access data in data event, I want to show data for the next date in the collection JadwalBooking.
Schema:
"keterangan" : "keterangan di rubah",
"dataevent" : {
"time_start" : 60,
"time_end" : 660,
"_id" : ObjectId("5b3da607acddef1c24317dd0"),
"name" : "event 1",
"description" : "lorem ipsum, lorem ipsum",
"date" : ISODate("2018-11-25T00:00:00.000Z")
}
Query:
const data = await JadwalBooking.aggregate([
{
$match: {
dataevent: {
$elemMatch: {
date: {
$gte: new Date(new moment().format("YYYY-MM-DD")),
}
}
}
}
},
{
$project:
{
_id: 1,
dataevent: 1,
keterangan: 1,
}
},
{
$sort: { date: 1 }
}
]);
You need to use dot notation for query and sort in datevent date:
const data = await JadwalBooking.aggregate([
{
$match: {
"dataevent.date": {
$gte: new Date(new moment().format("YYYY-MM-DD"))
}
}
},
{
$project:
{
_id: 1,
dataevent: 1,
keterangan: 1,
}
},
{
$sort: { "dataevent.date": 1 }
}
]);
You dont need to use $elemMatch for your case, $elemMatch is used, when you want to query a specific Object from an array of Objects, and return only matched Object from the array.
In your case a simple query with "." notation will work.
Try this:
const data = await JadwalBooking.aggregate([
{
$match: {
dataevent.date: {
$gte: new Date(new moment().format("YYYY-MM-DD"))
}
}
},
{
$project:
{
_id: 1,
dataevent: 1,
keterangan: 1,
}
},
{
$sort: { date: 1 }
}
]);
As not mentioned specifically to the aggregation,
db.collection
.find({"dataevent.date" : {$gt : new Date(new moment().format("YYYY-MM-DD"))}})
.sort({"dataevent.date": 1})
One more thing is:
Based on your schema you really don't need to use $project too. As you are retrieving whole data.
Note:- $elemMatch is used for Arrays, you need to use dot notation.
const data = await JadwalBooking.aggregate([
{
$match: {
"dataevent.date": {
$gte: new Date(new moment().format("YYYY-MM-DD"))
}
}
},
{
$sort: { date: 1 }
}
]);

Categories