I'm using mongodb with mongoose in nodejs, I have my schema like this:
const UserSchema = new mongoose.Schema({
username: {
type: String,
unique: true,
require: true
},
balances: {
dolar: {
manualBalance: {
type: Number,
select: false
},
paidBalance: {
type: Number,
select: false
}
}
}
});
I try to do this:
router.post('/get_user_balance', async (req, res) => {
const { username } = req.body;
User.findOneAndUpdate({ username: username }, { $set: { balances: {dolar: {manualBalance: '1.50' }}}});
const balances = await User.findOne({ username }).select('+balances.dolar.manualBalance');
console.log(balances);
}
and I get the nodejs error:
(node:22826) UnhandledPromiseRejectionWarning: MongoError: Projection cannot have a mix of inclusion and exclusion.
I believe it is% w / o% wrong. So I also tried without the select and I removed the "select: false" from my schema, I resisted the database and re-created a user, and I receive only the username from console.log, instead of also receiving the "balances" field. p>
{
_id: 5ab00b7a92fe402bb85f407f,
username: 'Jerildo',
__v: 0
}
I'm registering users like this:
const user = await User.create(req.body);
where req.body is: .select('+balances.dolar.manualBalance');
I know it must be simple thing that I'm missing out on not knowing mongodb or mongoose so well, I've always worked with Redis, this is new here for me. So I ask for a force here.