I'm trying to get an object which has isDraft value true, but I'm also getting objects which have isDraft value false. I need only objects having isDraft value true. I have tried all possible ways but am not able to find a solution for this. Can anyone help me with this?
Below are the schema, query and response.
Schema
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const Contract = new Schema({
name: {
type: String,
unqiue: true,
required: true
},
version: [
{
no: {
type: Number,
required: true
},
sections: [
{
sectionName: {
type: String,
required: true
},
clause: [{
description: {
type: String,
required: true
},
}]
}
],
approvedBy: [
{
user: {
type: Schema.Types.ObjectId,
ref: 'user'
},
}
],
acceptedBy: [
{
name: {
type: String,
},
eamil: {
type: String,
},
}
],
isDraft: {
type: Boolean,
required: true
},
date: {
type: Date,
default: Date.now
}
}
],
createdBy: {
type: Schema.Types.ObjectId,
ref: 'user',
required: true
},
});
module.exports = mongoose.model('contract', Contract);
Query
query = {
$and: [
{ createdBy: clientAdminDetails._id },
{ "version.isDraft": true }
],
};
await Contract
.find(query)
.skip(req.body.noOfItems * (req.body.pageNumber - 1))
.limit(req.body.noOfItems)
.exec((err, contract) => {
if (err) {
return res.json(err);
}
Contract.countDocuments(query).exec((count_error, count) => {
if (err) {
return res.json(count_error);
}
return res.json({
total: count,
page: req.body.pageNumber,
pageSize: contract.length,
contracts: contract
});
});
});
Response
{
"total": 1,
"page": 1,
"pageSize": 1,
"contracts": [
{
"_id": "61449469775..",
"name": "Octavia Blankenship",
"version": [
{
"_id": "614496593cc..",
"sections": [
{
"_id": "61449469775..",
"sectionName": "Est dolore dolorem n Updated `1323",
"clause": [
{
"_id": "614494697..",
"description": "Numquam nostrud et a"
}
]
}
],
"isDraft": false,
"no": 1,
"approvedBy": [],
"acceptedBy": [],
"date": "2021-09-17T13:21:29.509Z"
},
{
"_id": "614496122904ee4e046fbee8",
"sections": [
{
"_id": "6144955a8c0061025499606f",
"sectionName": "Praesentium suscipit",
"clause": [
{
"_id": "6144955a8c00610254996070",
"description": "Velit aperiam ut vel"
}
]
}
],
"isDraft": true,
"no": 2,
"approvedBy": [],
"acceptedBy": [],
"date": "2021-09-17T13:20:18.128Z"
}
],
"createdBy": "614367e980b29e6c...",
"__v": 0
}
]
}
This is why using your query you are telling mongo "Give me a document where createdBy is desired id and version.isdraft is true" So, as the DOCUMENT contains both values, is returned, even existing false into the array.
To solve this you have many ways.
First one is using $elemMatch into projection (docs here). But using this way only the first element is returned, so I think you prefer other ways.
So you can use an aggregation query using $filter like this:
First $match by values you want (as in your query).
Then override version array filtering by values where isDraft = true.
db.collection.aggregate([
{
"$match": {
"createdBy": "",
"version.isDraft": true
}
},
{
"$set": {
"version": {
"$filter": {
"input": "$version",
"as": "v",
"cond": {
"$eq": [
"$$v.isDraft",
true
]
}
}
}
}
}
])
Example here
Related
This is mongoose schema of route model
const routeSchema = new mongoose.Schema({
route: {
type: [{
stationCode: {
type: String,
required: true,
uppercase: true,
validate: {
validator: async function(val) {
const doc = await Station.findOne({
code: val,
});
if (!doc) return false;
return true;
},
message: `A Station with code {VALUE} not found`,
},
},
distanceFromOrigin: {
type: Number,
required: [
true,
'A station must have distance from origin, 0 for origin',
],
},
}, ],
validate: {
validator: function(val) {
return val.length >= 2;
},
message: 'A Route must have at least two stops',
},
},
}, {
toJSON: {
virtuals: true
},
toObject: {
virtuals: true
},
});
This schema has a field route as array of documents,
each document has a stationCode,
I want to query for all the documents which has "KMME" and "ASN" stationCode in the specified order.
Below is example of a document created with this schema
{
"_id": {
"$oid": "636957ce994af955df472ebc"
},
"route": [{
"stationCode": "DHN",
"distanceFromOrigin": 0,
"_id": {
"$oid": "636957ce994af955df472ebd"
}
},
{
"stationCode": "KMME",
"distanceFromOrigin": 38,
"_id": {
"$oid": "636957ce994af955df472ebe"
}
},
{
"stationCode": "ASN",
"distanceFromOrigin": 54,
"_id": {
"$oid": "636957ce994af955df472ebf"
}
}
],
"__v": 0
}
Please suggest a query for this problem or another schema definition for this problem
One simple option is:
db.collection.aggregate([
{$match: {$expr: {$setIsSubset: [["ASN", "KMME"], "$route.stationCode"]}}},
{$set: {
wanted: {$first:{
$filter: {
input: "$route",
cond: {$in: ["$$this.stationCode", ["ASN", "KMME"]]}
}
}}
}},
{$match: {"wanted.stationCode": "KMME"}},
{$unset: "wanted"}
])
See how it works on the playground example
I have the following MySQL query in which I have done sum profit which I have fields like: rate, credit(money)
SELECT SUM((credit*(100-rate))/100) FROM roznamcha WHERE (accountNum=$id AND rate!=0.0)'
I have written the following query in mongodb using node.js but it returns null whoever I have some data in my database
const profit=await roznamcha.aggregate([
{
$match:{
rate:{$ne:0}
}
},
{
$group:{
_id :'$accountNum',
}
},
{
$addFields:{
resultMultiply:{
$divide:[
{$multiply:['$credit','$rate-$100']},100
]
},
sumcredit:{
$sum:'$resultMultiply'
}
} }
])
res.status(201).json({
status:'success',
data:{
profit
}
})
My output:
{
"status": "success",
"data": {
"profit": [
{
"_id": "612deac8fbc8ef21a0fa4ea7",
"resultMultiply": null,
"sumcredit": 0
},
{
"_id": "612223327e2af83a4cec1272",
"resultMultiply": null,
"sumcredit": 0
}
]
}
my schema:
const mongoose = require('mongoose');
const roznamchaSchem=mongoose.Schema({
accountNum: {
type:mongoose.Schema.ObjectId,
ref:'account',
required: ['please specify this record is from who', true],
},
credit: {
type: Number,
default: 0
},
debit: {
type: Number,
default: 0
},
rate: {
type: Number,
default: 0
},
description:{
type:String,
required:['description can not be empty',true],
minlength: 8
},
issueDate:{
type: Date,
required:['add an valide date',true]
}
});
roznamchaSchem.index({accountNum:-1});
const Roznamcha=mongoose.model('roznamcha',roznamchaSchem);
module.exports=Roznamcha;
and my example of document:
id:612f533e8eb5533f303966e4
credit:50
debit:0
rate:2
accountNum:612deac8fbc8ef21a0fa4ea7
description:"this it for you"
issueDate:6543-01-01T00:00:00.000+00:00
can anyone guide me in solving this query?
Besides #Joe and #Nenad answer the error for the subtraction: '$rate-$100';
You need to re-position your logic structure.
Perform calculation: SUM((credit*(100-rate))/100).
Group by $accountNum and aggregate SUM for resultMultiply.
db.collection.aggregate([
{
$match: {
rate: {
$ne: 0
}
}
},
{
$addFields: {
resultMultiply: {
$divide: [
{
$multiply: [
"$credit",
{
"$subtract": [
100,
"$rate"
]
}
]
},
100
]
}
}
},
{
$group: {
_id: "$accountNum",
total: {
$sum: "$resultMultiply"
}
}
}
])
Output
[
{
"_id": 2,
"total": 1.5
},
{
"_id": 1,
"total": 5.5
}
]
Sample MongoDB playground
'$rate-$100' is referring to a field named "rate-$100". You probably meant to subtract using
{$subtract: [ 100, "$rate"]}
You can not use mathematical operator minus for substraction, you have to use $subtract aggregation operator.
resultMultiply: {
$divide: [{
$multiply: [
"$credit",
{ $subtract: [ 100, "$rate" ] }
]
},
100
]
}
For a project where we have actions and donations. We store the donations in an array in the related action. For the connection we use Mongoose.
The schema for an action is as follows, for readability I've removed some fields which are not related to this problem:
const donationSchema = new Schema(
{
id: {
type: String,
unique: true,
required: true,
index: true,
},
amount: { type: Number },
status: {
type: String,
enum: ['pending', 'collected', 'failed'],
default: 'pending',
},
},
{ timestamps: true, versionKey: false, _id: false },
);
const schema = new Schema(
{
donations: { type: [donationSchema], default: [] },
target: { type: Number, default: 0 },
collected: { type: Number, default: 0 },
},
{
timestamps: true,
versionKey: false,
},
);
const Action = model<IAction>('Action', schema);
Let say I have an Action with three donations, one in every state:
{
"_id": "6098fb22101f22cfcbd31e3b"
"target": 10000,
"collected": 25,
"donations": [
{
"uuid": "dd90f6f1-56d7-4d8b-a51f-f9e5382d3cd9",
"amount": 25,
"status": "collected"
},
{
"uuid": "eea0ac5e-1e52-4eba-aa1f-c1f4d072a37a",
"amount": 10,
"status": "failed"
},
{
"uuid": "215237bd-bfe6-4d5a-934f-90e3ec9d2aa1",
"amount": 50,
"status": "pending"
}
]
}
Now I want to update the pending donation to collected.
This would be
Action.findOneAndUpdate(
{
_id: '6098fb22101f22cfcbd31e3b',
'donations.id': '215237bd-bfe6-4d5a-934f-90e3ec9d2aa1',
},
{
$set: {
'donations.$.status': 'collected',
},
},
{
upsert: false,
returnOriginal: false,
}
).then((action) => console.log(action);
I want to update the status to collected, but also update the collected so that it is the same as all the donations with status equal to collected. I thought of using the $inc operator, but this keeps saying that donations.$.amount is not a number and therefore not able to increment collected.
Is there a way to do this in the same update call? The reason why I cannot get the object and just count collected amount is that maybe two donation callbacks occur at the same time, so we don't want the to overwrite the previous given amount.
This aggregation can help you I believe:
db.collection.aggregate([
{
"$match": {
_id: "6098fb22101f22cfcbd31e3b"
}
},
{
"$set": {
"donations.status": {
"$reduce": {
"input": "$donations",
"initialValue": {
uuid: "215237bd-bfe6-4d5a-934f-90e3ec9d2aa1"
},
"in": {
$cond: [
{
$eq: [
"$$this.uuid",
"$$value.uuid"
]
},
"collected",
"$$this.status"
]
}
}
}
}
},
{
"$set": {
"collected": {
"$reduce": {
"input": "$donations",
"initialValue": "$collected",
"in": {
$cond: [
{
$eq: [
"$$this.status",
"collected"
]
},
{
$sum: [
"$$value",
"$$this.amount"
]
},
"$$value"
]
}
}
}
}
}
])
Edit: Above aggregation wasn't properly update status field to "collected" dunno why..
But update query below should work. I couldn't test it too. So, please let me know if something goes wrong.
db.collection.update({
"_id": "6098fb22101f22cfcbd31e3b"
},
{
"$set": {
"donations.$[element].status": "collected",
"$inc": {
"donations.$[element].amount": {
"$cond": [
{
"$eq": [
"donations.$[element].status",
"collected"
]
},
"donations.$[element].amount",
"collected"
]
}
}
}
},
{
"arrayFilters": [
{
"element.uuid": "215237bd-bfe6-4d5a-934f-90e3ec9d2aa1"
}
]
})
i'm new to MongoDB and trying to build a simple chat app using Node.Js and MongoDB using mongoose Js. and i'm stuck here for last 2 days so need help!!
Tested most of the related answer of stack overflow but not getting desirable result.
What i want to achieve is something similar we see in chat application like Facebook Messenger and whatsApp web where in one side we see all our conversation list which show last message and person profile.
an example here http://roba.laborasyon.com/demos/dark/ (left sidebar)
this is how my model look like
const chat = new Schema({
to:{
type:Schema.Types.ObjectId,
ref:"User",
required:true,
},
from:{
type:Schema.Types.ObjectId,
ref:"User",
required:true,
},
seenbySender:Boolean,
seenByReceiver:Boolean,
isFile:{
type:Boolean,
default:false
},
file:{
type:Object,
required:false,
},
text:String,
},{timestamps:true});
In My Controller or route file (this is not my code but i tried similar logic they all return empty array or error)
exports.test = (request,response,next) => {
let {user} = request.body;
const u=await User.findById(user);
//Chat.find({from:user}).populate('from').populate('to').then(r => response.json(r));
//user ="5f46319ac483a43d98ae3626";
Chat.aggregate([
{$match:{$or:[{"to":user},{"from":user}]}},
{
$group:{"_id":
{
"text":{
$cond:[
{
$gt:[
{$substr:["$to",0,1]},
{$substr:["$from",0,1]}]
},
{$concat:["$to"," and ","$from"]},
{$concat:["$from"," and ","$to"]}
]
}
},
"text":{$first:"$text"}
}
}]).then(result => {
response.json(result)
}).catch(error => {
console.log("[[error]]",error)
response.json({error})
});
}
Here is data i'm working with (exported JSON file)
[
{
"_id": {
"$oid": "5f4a3ae0a7ff491f3024668e"
},
"isFile": false,
"to": {
"$oid": "5f46325ec483a43d98ae3627"
},
"from": {
"$oid": "5f46319ac483a43d98ae3626"
},
"text": "Hi John,Yash here!",
"createdAt": {
"$date": "2020-08-29T11:24:16.416Z"
},
"updatedAt": {
"$date": "2020-08-29T11:24:16.416Z"
},
"__v": 0
},
{
"_id": {
"$oid": "5f4a3affa7ff491f3024668f"
},
"isFile": false,
"to": {
"$oid": "5f46319ac483a43d98ae3626"
},
"from": {
"$oid": "5f46325ec483a43d98ae3627"
},
"text": "hello Yash - John",
"createdAt": {
"$date": "2020-08-29T11:24:47.519Z"
},
"updatedAt": {
"$date": "2020-08-29T11:24:47.519Z"
},
"__v": 0
},
{
"_id": {
"$oid": "5f4a3b25a7ff491f30246690"
},
"isFile": false,
"to": {
"$oid": "5f4632c8c483a43d98ae3628"
},
"from": {
"$oid": "5f46319ac483a43d98ae3626"
},
"text": "Hello Don, Yash this side.",
"createdAt": {
"$date": "2020-08-29T11:25:25.067Z"
},
"updatedAt": {
"$date": "2020-08-29T11:25:25.067Z"
},
"__v": 0
}
]
So what i need is last message of user he chatted with, with the user reference. in this case for Id: 5f46319ac483a43d98ae3626 the last 2 objects should be render
Thanks a lot!!
You can try using $split to get limited records,
db.collection.aggregate([
{
$match: {
$or: [
{ "to": ObjectId("5f46319ac483a43d98ae3626") },
{ "from": ObjectId("5f46319ac483a43d98ae3626") }
]
}
},
// for descending order
{ $sort: { updatedAt: -1 } },
{
$group: {
_id: {
$cond: [
{ $eq: ["$to", ObjectId("5f46319ac483a43d98ae3626")] },
{ $concat: [{ $toString: "$to" }, " and ", { $toString: "$from" }] },
{ $concat: [{ $toString: "$from" }, " and ", { $toString: "$to" }] }
]
},
updatedAt: { $first: "$updatedAt" },
// push messages
messages: { $push: "$$ROOT" }
}
},
// order by descending order
{ $sort: { updatedAt: -1 } },
// limit to 2 messages only
{ $addFields: { messages: { $slice: ["$messages", 2] } } }
])
Playground
for joining user data you can use $lookup
I guess you can use this schema for a chat
MessageSchema = new mongoose.Schema({
from: {
type: string,
required: true,
},
to: {
type: string,
required: true,
},
time: {
type: Date,
default: Date.now(),
},
message: {
type: string,
required: true,
},
});
ChatSchema = new mongoose.Schema({
firstUserId:{
type: mongoose.Schema.Types.ObjectId,
required: true,
},
secondUserId:{
type: mongoose.Schema.Types.ObjectId,
required: true,
},
Chat: MessageSchema,
})
and data will be as this way
[
{
firstUserId: "hjakdsf323275lks",
secondUserId: "asdfe2342232aas",
Chat: [
{
from: "hjakdsf323275lks",
to: "asdfe2342232aas",
time: "18/7/2020 20:06:09",
message: "Hi ",
},
{
from: "asdfe2342232aas",
to: "hjakdsf323275lks",
time: "18/7/2020 21:07:09",
message: "hello ",
},....
],
},
];
I'm getting throw trying to populate related fields using aggregation pipeline in mongodb, plain relations works as well (I mean oid reference to oid in other collection) but what happens when you have an object array that one of its properties reference a sub document. If I wasn't clear, here a little representation.
Suppose I have the following schema:
Profile {
_id: {
type: mongoose.Schema.Types.ObjectId
},
Gender: {
type: mongoose.Schema.Types.ObjectId,
ref: "Gender"
},
PhoneNo: [
Value: {
type: String
},
PhoneType: {
type: mongoose.Schema.Types.ObjectId,
ref: "PhoneType"
}
]
}
PhoneType {
_id: {
type: mongoose.Schema.Types.ObjectId
},
Name: {
type: String
}
}
Gender {
_id: {
type: mongoose.Schema.Types.ObjectId
},
Name: {
type: String
}
}
So, I would like to get results like:
{
_id: $oid,
Gender: {Value:"Masculine"},
PhoneNo: {
Value: "000000",
PhoneType: {
_id: $oid
Name: "Cell"
}
}
},
{
_id: $oid,
Gender: {Value:"Feminine"},
PhoneNo: {
Value: "999999",
PhoneType: {
_id: $oid
Name: "Work"
}
}
}
Lookup in Gender works good, but when I try to lookup PhoneNo then I lost the value property.
What I'm getting is:
Pay attention to the field/property Value, is lost.
{
_id: $oid,
Gender: {Value:"Masculine"},
PhoneNo: [{
PhoneType: {
_id: $oid
Name: "Cell"
}
}]
},
{
_id: $oid,
Gender: {Value:"Feminine"},
PhoneNo: [{
PhoneType: {
_id: $oid
Name: "Work"
}
}]
}
Here is the code that I used:
{
from: 'PhoneType',
'let': {"ids":"$PhoneNo.PhoneType"},
"pipeline": [
{ "$match": { "$expr": { "$in": ["$_id", "$$ids"] } } },
],
as: "PhoneNo"
}
How can I do that? :S
So this is a walkthrough for your problem, we also include Values in lookup let declaration it'll be an array because it's stored in an array and in project stage we'll fetch indexOf Values array where $$ROOT 's id and id match
https://mongoplayground.net/p/UUXus3N3ncw
Input collections:
"user": [
{
_id: {
type: "1"
},
Gender: 12,
PhoneNo: [
{
Value: "name",
PhoneType: 21
},
{
Value: "name2",
PhoneType: 212
}
]
}
],
"gender": [
{
"_id": 12,
"Name": "Male"
}
],
"PhoneType": [
{
"_id": 21,
name: "Sus",
val: "750"
},
{
"_id": 212,
name: "Suss",
val: "7500"
}
]
Aggregate Pipeline:
user.aggregate([
{
$lookup: {
from: "PhoneType",
"let": {
"ids": "$PhoneNo.PhoneType",
"val": "$PhoneNo.Value",
},
"pipeline": [
{
"$match": {
"$expr": {
"$in": [
"$_id",
"$$ids"
]
}
}
},
{
$project: {
_id: 0,
Value: {
$arrayElemAt: [
"$$val",
{
$indexOfArray: [
"$$ids",
"$$ROOT._id"
]
}
]
},
PhoneType: "$$ROOT"
}
}
],
as: "PhoneNo"
}
}
])
Output Result:
[
{
"Gender": 12,
"PhoneNo": [
{
"PhoneType": {
"_id": 21,
"name": "Sus",
"val": "750"
},
"Value": "name"
},
{
"PhoneType": {
"_id": 212,
"name": "Suss",
"val": "7500"
},
"Value": "name2"
}
],
"_id": {
"type": "1"
}
}
]