Compare 2 values from same doc in mongo - javascript

I want to compare 2 values in same doc id
For example
{
"_id" : ObjectId("5f180441ad1cd40008dc6a5a"),
"fcount" : 5,
"key" : "27b6e581-796c-4f3a-882b-0e0c2a0a8a64",
"student_id" : "5f0ffbcdd67d70c1a3b143aa",
"__v" : 0,
"dcount" : 5
}
I have this doc, Now If I want to check fcount and dcount is same then return true. OR false
How can I do this with mongoose query?
update:
const result = await execute_reports_model.find({ _id: _id}).lean().cursor({batchSize: 10}).eachAsync(async ({fcount, dcount}) => {
if (fcount === dcount) {
// true Logic
} else {
// false logic
}
}, { parallel: 10})
So here I will be passing the ID in find. and then compare values that I found from that ID. values are fcount and dcount

Using Mongoose's Model.aggregate you can compare a document's two fields and return a boolean:
const result = await model.aggregate([
{ $match: { _id: id } },
{ $project: { _id: 0, isMatch: { $eq: [ "$fcount", "$dcount" ] } } }
])
result value: { "isMatch" : true }

This precursor code will help you to solve your problem. It will allow you to find the necessary documents and then deal with all true and false cases.
{batchSize: 10} and {parallel:10} will help you to do your task at parallel. You could easily modify them.
Don't forget to add callback or handle all cases somehow, as you need it to.
await collection_name.find({ your_query: here}).lean().cursor({batchSize: 10}).eachAsync(async ({fcount, dcount}) => {
if (fcount === dcount) {
//true case
} else {
//false case
}
}, { parallel: 10})

Related

How to keep decreasing value in mongo until is 0

singleObj = await Objects.findByIdAndUpdate({ _id: req.body.id, }, { $inc: { 'total_obj': -1, 'total_stuff': 1 }, }, { new: true })
The user clicks a button and the value of 'total_obj' gets decreased by one. The value doesn't have to be less than 0.
I have tried to do this:
singleObj = await Objects.findByIdAndUpdate(
{ _id: req.body.id, "total_obj": { "$lt": 0 } },
{ "$set": { "total_obj": 0 } }
);
But this messes up every time I load the page and I have the values set to 0.
I also added on the definition on the schema:
total_obj: {
type: Number,
required: true,
min: 0
},
I assume you meant that you don't want your value to be lesser than 0.
You would need to use $gt operator and while you used $inc properly in the first findByIdAndUpdate you didn't use it in the second one.
Also, we are not looking only for id so we should use findOneAndUpdate instead.
singleObj = await Objects.findOneAndUpdate(
{ _id: req.body.id, "total_obj": { "$gt": 0 } },
{ $inc: { "total_obj": -1 } }
);
Try to fetch the Objects instance first and update the value only if > 0:
const singleObj = await Objects.findById(req.body.id)
if (!singleObj) // Error, obj not found
if (singleObj.total_obj > 0) {
singleObj.total_obj = singleObj.total_obj-1
await singleObj.save()
} else {
// `total_obj` is already zero
}

Sorting MongoDB result by another MongoDB result is returning [null, null, null]

I'm performing two MongoDB queries, and then I want to synchronize the resulting arrays, to make sure they are in the same order.
The first array is a set of (20) questions ids (this is the correct order):
q_id_arr: [
"5f86da2d37e3d200040ba523",
"5f86b6ce37e3d200040ba4c6",
"5ffc4abea04f3c0004e46cf3",
"5f86b66537e3d200040ba4c5",
"5f87f368554f370004ed17b4",
"5f86e48c37e3d200040ba53c",
"5ffc4dc4a04f3c0004e46d0b",
"5f86e19037e3d200040ba534",
"5f86aaa237e3d200040ba49b",
"5ffc479ba04f3c0004e46ce0",
"5f86b9dc37e3d200040ba4d2",
"5f85828e0e1bd30004361430",
"5f8700c937e3d200040ba548",
"5f86d81737e3d200040ba51c",
"5f8708d237e3d200040ba568",
"5f87060d37e3d200040ba55c",
"5f857dac0e1bd3000436141c",
"5f85703e0e1bd300043613ec",
"5f87e9d4554f370004ed178e",
"5f8073c04ad88e00041f015f"
]
The second array is a set of (20) results associated with the question ids:
team_trends: [
{
"_id":"5f87e9d4554f370004ed178e",
"positive":0.93,
"engaged":0.558
},
{
"_id":"5f86e19037e3d200040ba534",
"positive":0.585,
"engaged":0.567
},
{
"_id":"5f85828e0e1bd30004361430",
"positive":0.7,
"engaged":0.666
},
{
"_id":"5f8073c04ad88e00041f015f",
"positive":0.31,
"engaged":0.30999999999999994
},
{
"_id":"5f87f368554f370004ed17b4",
"positive":0.5449999999999999,
"engaged":0.57
},
{
"_id":"5f86b6ce37e3d200040ba4c6",
"positive":0.855,
"engaged":0.46599999999999997
},
{
"_id":"5f857dac0e1bd3000436141c",
"positive":0.92,
"engaged":0.524
},
{
"_id":"5f85703e0e1bd300043613ec",
"positive":0.15,
"engaged":0.39
},
{
"_id":"5f86aaa237e3d200040ba49b",
"positive":0.15000000000000002,
"engaged":0.584
},
{
"_id":"5f86b66537e3d200040ba4c5",
"positive":0.37,
"engaged":0.386
},
{
"_id":"5f86e48c37e3d200040ba53c",
"positive":0.615,
"engaged":0.548
},
{
"_id":"5ffc479ba04f3c0004e46ce0",
"positive":0.42000000000000004,
"engaged":0.583
},
{
"_id":"5f86b9dc37e3d200040ba4d2",
"positive":0.68,
"engaged":0.662
},
{
"_id":"5f86d81737e3d200040ba51c",
"positive":0.03,
"engaged":0.516
},
{
"_id":"5f87060d37e3d200040ba55c",
"positive":0.14,
"engaged":0.454
},
{
"_id":"5f86da2d37e3d200040ba523",
"positive":0.47,
"engaged":0.41500000000000004
},
{
"_id":"5f8708d237e3d200040ba568",
"positive":0.17,
"engaged":0.76
},
{
"_id":"5ffc4dc4a04f3c0004e46d0b",
"positive":0.395,
"engaged":0.53
},
{
"_id":"5ffc4abea04f3c0004e46cf3",
"positive":0.365,
"engaged":0.679
},
{
"_id":"5f8700c937e3d200040ba548",
"positive":0.93,
"engaged":0.6980000000000001
}
]
I want to reorganize team_trends into the same order as q_id_arr
Here is the code I'm using (following this SO Answer):
let c = [];
q_id_arr.forEach((q_oid => c.push(team_trends.find((obj => obj._id == q_oid)))));
However when I print console.log("the result of c"+ c) I get this result:
the result of c: [null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null,null]
Is this the right approach? Any suggestions are appreciated!
More Details:
Before this step I acquired the q_id_arr through mapping over an aggregate result, like this:
let q_id_arr = await user_trends.map(({ question_oid }) => question_oid)
When I tested console.log(typeof q_id_arr) it returned object.
How can I sort through the object??
Final code that fixed the issue.
user_trends.forEach((user => c.push(team_trends.find((obj => obj._id.toString() === user.question_oid.toString())))));
A couple of points to note here:
If you are using mongoose then it already returns an array from the aggregate function.
Mongoose uses the MongoDB NodeJs native driver at its core. In core driver ObjectId has a function .equals(otherId). It is always best to use this function for id comparisons.
References:
Comparing mongoose _id and strings
.aggregate(...).toArray is not a function
https://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html#equals
You can try this using the arrays map method:
let sorted_team_trends = q_id_arr.map( q => team_trends.find(t => t._id === q) );
Assuming the two arrays are defined as fields like this:
let q_id_arr = [
"5f86da2d37e3d200040ba523",
"5f86b6ce37e3d200040ba4c6",
"5ffc4abea04f3c0004e46cf3",
...
];
let team_trends = [
{
"_id" : "5f87e9d4554f370004ed178e",
"positive" : 0.93,
"engaged" : 0.558
},
{
"_id" : "5f86e19037e3d200040ba534",
"positive" : 0.585,
"engaged" : 0.567
},
...
]

How to add a MongoDB match argument if it comes from frontend and not add any argument if it doesn't?

I am creating an aggregation in MongoDB using NodeJS. When the resolver function is called with an argument, I want it to be added to MongoDB match function and if it doesn't exist, then there will be no addition. The problem I am facing is that if there is no addition, no results are coming and if there is addition then the query is not coming properly. It is coming like this: phaseMatch = 'phase': { $in: CAT,MOD }. how do I make it come like phaseMatch = 'phase': { $in: ['CAT','MOD'] }? Is there any way to do this conditional only in the MongoDB aggregation and not use any JS?
async WeeklyTable(_, { batchSize, phase }) {
let res = [];
let phaseMatch = "";
if(phase) phaseMatch = "'phase': { $in: " + phase + " }";
return await collection.aggregate([{
$match: {
"segment": {
$exists: true,
$ne: null
},
phaseMatch
}
}];
}
There are many ways to dynamically build a query condition, here is a quick example:
async WeeklyTable(_, { batchSize, phase }) {
const res = [];
const matchCond = {
"segment": {
$exists: true,
$ne: null
},
}
if(phase) {
matchCond.phase = {$in: phase}
};
return await collection.aggregate([{
$match: matchCond
}];
}
Not sure exactly what you're trying to do with creating it as a string, the Mongo driver doesn't parse string arguments as query conditions.
EDIT
Without any javascript you could do:
db.collection.aggregate([
{
$match: {
"segment": {
$exists: true,
$ne: null
},
$expr: {
"$setIsSubset": [
[
"$phase"
],
{
$ifNull: [
phase,
[
"$phase"
]
]
}
]
}
}
}
])

How to add dynamic elements to an object in typescript

I am sorry if I am asking a very basic question, I have done some research over the internet but not getting anything useful.
I have a typescript object like :
var productIds=["one","two","three"];
let searchfilter = {
or: [{
id: { match:productids['0'] }
},{
id: { match:productids['1'] }
},{
id: { match:productids['2'] }
}]
};
My productIds can be dynamic and may hold different counts of values.
How can I create the same structure for a dynamic number of values. I tried forEach, but not sure about the syntax.
productids.forEach(function(value){
// not sure if this is right syntax, I am not getting desired results.
searchfilter.or = { id: { match:value }};
});
Can you help me with it?
You can create your full or array with a simple .map() :
var productIds = ["1", "2", "3"];
let searchfilter = {
or : productIds.map( n => ({ id : { match : productIds[n] } }))
};
However Mongo (which I believe you are using) has a $match method that's made to match a list :
{
$match: {
productIds: {
$in: productIds
}
}
}
I'll keep it as simple as I can
var productIds=["one","two","three"];
let searchfilter = productIds.map(p => {
return {id: { match: p }};
});
// function
addNewProduct(id: string) {
this.searchfilter.push({id: { match: id }});
}

JS: $addToSet or $pull depending on existing/missing value

I need to add or remove an ID from an array (target), depending if it is already existing. This is how I am doing this:
var isExisting = Articles.findOne({ _id }).target.indexOf(mID) > -1
if (isExisting === false) {
Articles.update(
{ _id },
{ $addToSet: { target: mID } }
)
} else if (isExisting === true) {
Articles.update(
{ _id },
{ $pull: { target: mID } }
)
}
Is it possible to do this in a better way - without doing if/else and min. two db operations?
Mongoose operations are asynchronous, so you need to wait for its callback to get the document.
// find the article by its ID
Articles.findById(_id, function (err, article) {
// make appropriate change depending on whether mID exist in the article's target
if (article.target.indexOf(mID) > -1)
article.target.pull(mID)
else
article.target.push(mID)
// commit the change
article.save(function (err) {
});
})
Although you are doing if/else, you are doing 2 operations.
here is my suggestion
let isExisting = Articles.findOne({ _id: _id, target : mID}) //mongo can search for mID in array of [mIDs]
let query = { _id : _id };
let update = isExisting ? { $pull: { target: mID } } : { $addToSet: { target: mID } };
Articles.update(query, update);
is it better and clearer now?

Categories

Resources