2

I have two model files Bank Deposits and Sub account details. From Sub account details I want to get current balance to Bank deposits . I want to do this in mongodb and node js. I am currently aggreagate lookup operation but it is showing the array as empty.

BankdepositModel.js

var mongoose = require("mongoose");

var BankDepositsSchema = new mongoose.Schema(
  {
    source_sub_account: Array,
    amount: Number,
    cheque_notes: Number,
    to_bank_account: Array,
    amount_in_words: String,
    bank_Ref_no: String
  },
  {
    timestamps: true
  }
);

module.exports = mongoose.model("BankDeposits", BankDepositsSchema);

Subaccountdetailsmodel.js

var mongoose = require("mongoose");

var SubAccountDetailsSchema = new mongoose.Schema(
  {
    sub_account_name: String,
    current_balance: Number,
    account: Array
  },
  {
    timestamps: true
  }
);

module.exports = mongoose.model("SubAccountDetails", SubAccountDetailsSchema);

Controller.js

var BankDeposits = require("../model/bankdepositmodel");
var SubAccountDetails = require("../model/subaccountsmodel.js");

exports.create1 = (req, res) => {
  var BankDeposit = new BankDeposits({
    source_sub_account: req.body.source_sub_account,
    amount: req.body.amount,
    cheque_notes: req.body.cheque_notes,
    to_bank_account: req.body.to_bank_account,
    amount_in_words: req.body.amount_in_words,
    bank_ref_no: req.body.bank_ref_no
  });

  BankDeposit.save()
    .then(data1 => {
      res.send(data1);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message
      });
    });
};

//BankDeposit get
exports.find1 = (req, res) => {
  BankDeposits.aggregate([
    {
      $lookup: {
        from: "SubAccountDetails",
        localField: "current_balance",
        foreignField: "current_balance",
        as: "balance"
      }
    }
  ])
    .then(appdata => {
      res.status(200).send(appdata); //On successful fetch, server responds with status 200
    })
    .catch(err => {
      res.status(400).send(err); //On error, server responds with status 400
    });
};
//sub account details post
exports.createSubAccountDetail = (req, res) => {
  var SubAccountDetail = new SubAccountDetails({
    sub_account_name: req.body.sub_account_name,
    current_balance: req.body.current_balance,
    account: req.body.account
  });

  SubAccountDetail.save()
    .then(SubAccountDetail => {
      res.send(SubAccountDetail);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message
      });
    });
};

//sub account details get
exports.SubAccountDetail = (req, res) => {
  SubAccountDetails.find()
    .then(SubAccountDetails => {
      res.send(SubAccountDetails);
    })
    .catch(err => {
      res.status(500).send({
        message: err.message || "Some error occurred while retrieving regs."
      });
    });
};
4
  • 1
    SubAccountDetails must be subaccountsetails ?In from expression from: "SubAccountDetails" Commented Feb 11, 2020 at 7:57
  • not working ...current balance is not coming Commented Feb 11, 2020 at 10:07
  • I cannot see current_balance field in BankDeposit collection. From where it is coming? Commented Feb 11, 2020 at 10:44
  • it should be came from sub account details Commented Feb 11, 2020 at 12:50

1 Answer 1

2

You can get it like this

BankDeposits.aggregate([
    {
      $lookup: {
        from: "SubAccountDetails",
        localField: "source_sub_account",
        foreignField: "sub_account_name",
        as: "sub_acount"
      }
    }
  ])

Now you will get your complete sub_account object in sub_account property of returned data, current_balance will be in same

This is assuming that sub_account_name in SubAccountDetails & source_sub_account in BankDeposits are same

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.