Consider the following document schema:
var WalletSchema = new Schema({
name: {
type: String,
required: 'Kindly enter the name of the Coin'
},
.
.
.
coins: {
type: [CoinSchema]
}
});
var CoinSchema = new Schema({
name: {
type: String,
required: 'Kindly enter the name of the Coin'
},
symbol: {
type: String,
required: 'Kindly enter the symbol of the Coin'
}
.
.
.
});
Now consider the following function:
exports.edit_coin = function(req,res) {
Wallet.findOneAndUpdate(
{
name: req.params.wname,
"coins.symbol": req.params.symbol
},
{
$set: {
"coins.$": req.body,
last_updated: Date.now()
}
},{
new: true
},function(err,wallet) {
if (err)
res.send(err);
console.log(wallet);
res.json(wallet);
});
}
I only want to see elements of the coins array which are equal to the given symbol (req.params.symbol).
When I run the function with req.params.wname = Wallet, which is a wallet in my DB which has two coins in it one GOC and one BTC, and req.params.symbol = BTC with some JSON as the body I expect to only get BTC on console, but the following is what I get:
{
"_id": "61d2e17ebc07b26e3f3f692a",
"name": "Wallet",
"balance": 1042.8,
"coins": [
{
"name": "Go coin",
"symbol": "GOC",
"amount": 4,
"rate": 21.4,
"_id": "61d2e21a4b112f01592613d0"
},
{
"name": "Bitcoin",
"symbol": "BTC",
"amount": 2,
"rate": 50000,
"_id": "61d2e77c76fd08ea50a3aff5"
}
],
"last_updated": "2022-01-03T12:09:32.242Z",
"__v": 0
}
Why don't I only get bitcoin?