users.findOneAndUpdate
an anonymous user
·
MongoDB
const transaction = {
type: 'expense',
amount: 90,
wallet: 'bitcoin'
};
db.createCollection("users");
db.users.insertOne(
{
_id: ObjectId("65bea0174ac627b1ae79b664"),
username: "bob",
wallets:[
{
name: 'bitcoin',
balance: 100
},
{
name: 'eth',
balance: 75
}
],
transactions: [
{
type: 'expense',
amount: 25,
wallet: 'eth'
},
]
}
);
db.users.findOneAndUpdate(
{
_id: ObjectId("65bea0174ac627b1ae79b664"),
'wallets.name': transaction.wallet
},
{
$push: {
'transactions': transaction
},
$inc: {
'wallets.$.balance': -transaction.amount
}
}
);
db.users.find();
Output
Embed on website
To embed this program on your website, copy the following code and paste it into your website's HTML:
Comments
This comment belongs to a banned user and is only visible to admins.
This comment belongs to a deleted user and is only visible to admins.