How to update document with subdocument, or create new one if none are found - mongodb

I'm trying to create a new subdocument object in an array when a user calls a command OR update the existing document based on their id. However, everything I've tried either gives me errors or it overrides the existing subdocument with the current user which is not what I want.
enter image description here
Basically I want to add another object in the array ( "1": Object ) that is a second user tracking whether they've used the command or not.
I can't remember all variations on code, but the current code I'm using:
const query = {
_id: guild.id,
members : [{
_id: user.id
}]
}
const update = {
members: {
_id: user.id,
bot: user.bot,
commandUsed: true
}
}
const options = {upsert: true, new: true}
await mongo().then(async () => {
console.log('Updating to...in use')
try {
// Find the document
await commandUsageSchema.findOneAndUpdate(query, update, options, function(error, result) {
if (!error) {
// If the document doesn't exist
if (!result) {
// Create it
result = new userSchema;
}
// Save the document
result.save(function(error) {
if (!error) {
// Do something with the document
} else {
throw error;
}
})
}
})
is creating a duplicate key error which is frustrating. Here is the layout of my schemas:
const mongoose = require('mongoose')
const reqString =
{
type: String,
required: true
}
const userSchema = mongoose.Schema({
_id: reqString,
bot: Boolean,
commandUsed: Boolean
}, {unique: true})
const commandUseSchema = mongoose.Schema({
_id: reqString,
members: [userSchema]
})
module.exports = mongoose.model('command-usage-checker', commandUseSchema)
I'm using mongoose and mongoDB (of course) with javascript and DiscordJS.

Related

How find in mongoose by an array property

I have defined my Conversation scheme like this:
const { Schema, model } = require("mongoose");
const ConversationSchema = Schema(
{
members: {
type: Array,
},
},
{ timestamps: true }
);
module.exports = model("Conversation", ConversationSchema);
My problem is that when I want to create a conversation model I search first if there is already a conversation.
const newConversation = async (req, res = response) => {
try {
const { senderId, receiverId } = req.body;
const conversation = await Conversation.find({
members: { $in: [senderId, receiverId] },
});
if (conversation.length === 0) {
const dbConversation = new Conversation({
members: [senderId, receiverId],
});
await dbConversation.save();
return res.status(201).json({
ok: true,
conversation: dbConversation
});
} else {
return res.status(403).json({
ok: false,
msg: "Conversation already exist",
});
}
} catch (err) {
return res.status(500).json({
ok: false,
msg: "Please contact with administrator",
});
}
};
senderId and receivedId are the ids of the users that are in that conversation, but it doesn't work.
How can I make it check if there is already a conversation with both ids?
Per the comments, we came to understand that the thing that wasn't working about the current code was always taking the code path that returned the message that the "Conversation already exist". This meant that the following query was always returning data:
const conversation = await Conversation.find({
members: { $in: [senderId, receiverId] },
});
The logic here does not match the logic implied in the question. This syntax uses the $in operator to find documents whose members array has at least one of the values passed to it (here the senderId and the receiverId).
To instead find documents where both of those people are present in the members array, you want to use the $all operator instead:
const conversation = await Conversation.find({
members: { $all: [senderId, receiverId] },
});
Working Mongo Playground example here.

MongoDB, collection name appears to be wrong

When I try to call this request
const presidentModel = require('./modules/president.js')
app.get('/president', (req, res) => {
presidentModel.find({}, (err, result) => {
if (err) {
console.log(err)
} {
console.log(result)
}
})
})
It only returns an empty array []
then it creates a new collection with the name 'presidents'
Here is my Schema
const mongoose = require("mongoose")
const presidentSchema = new mongoose.Schema({
nickname: {
type: String,
required: true
},
fullname: {
type: String,
required: true,
},
votes: {
type: Number,
required: true
}
})
const president = mongoose.model("president", presidentSchema)
module.exports = president
The request should return the data on collection "president" but what it does is it creates a new collection with the name "presidents". I don't know where is it coming from tho
it is good practice to have your collections as plural, and therefore mongoose implicitly tries to make collections plural (as there are multiple items to be stored in them).
To override this, you can pass a third parameter to .model() with the name of the collection:
const president = mongoose.model("president", presidentSchema, "president")

Updating mongoose subSchema Object

My Schema is like this
const subSchema = new Schema({ /*...*/ })
const mainSchema = new Schema({
//...,
foo:{
type:subSchema,
default:{}
}
})
const Model = model('Model', mainSchema)
If I am doing this the whole foo get replaced by req.body
Model.findByIdAndUpdate(_id,{ foo:req.body }, { new:true,runValidators:true })
But I want that the only fields present in req.body get replaced and the rest remain same
You can create an variable that contains fields to update from req.body first. Something like:
let update = Object.keys(req.body).reduce((acc, cur) => {
acc[`foo.${cur}`] = req.body[cur];
return acc;
}, {});
Model.findByIdAndUpdate(_id, update,...
You case use mongoose projection :
Model.findOneAndUpdate(
{ _id, 'foo._id': fooId },
{ $set: { 'foo.$': req.body } }
);
Mongoose params projection :
https://mongoosejs.com/docs/api.html#model_Model-find
MongoDB projection :
https://www.mongodb.com/docs/manual/reference/operator/projection/positional/

Mongoose findOneAndUpdate on model

What's the purpose of a Mongoose schema if I'm doing an upsert with findOneAndUpdate call?
Everything I can find seems to indicate if I do a findOneAndUpdate, I need to reference the base schema instead of an instance.
Here's my setup:
const PersonSchema = new mongoose.Schema({
ssn: {
type: Number,
unique: true,
},
first: String,
last: String
})
const Person = mongoose.model("Person", PersonSchema)
const person = new Person({ssn: 123456789, first: "Foo", last: "Bar"})
If I just do a save (and that ssn exists already, I'll get a 'unique' violation).
person.save();
Instead, I'm finding out that I need to do something like
const options = { upsert: true, new: true }
const query = { ssn: 123456789 }
Person.findOneAndUpdate(
query,
{
ssn: 123456789,
first: "Foo",
last: "Bar"
},
options)
OR
const options = { upsert: true, new: true }
const query = { ssn: 123456789 }
const newPerson = Object.assign({}, person._doc)
// delete this so I don't get a conflict with Mongoose on the _id during insert
delete newPerson._id
Person.findOneAndUpdate(query, newPerson, options)
It seems like findOneAndUpdate doesn't care about the specific model (or instance) and is simply just a mechanism to get to the underlying MongoDB method.
Is that the case? Or am I missing something that should be obvious?

How to update embedded document in mongoose?

I've looked through the mongoose API, and many questions on SO and on the google group, and still can't figure out updating embedded documents.
I'm trying to update this particular userListings object with the contents of args.
for (var i = 0; i < req.user.userListings.length; i++) {
if (req.user.userListings[i].listingId == req.params.listingId) {
User.update({
_id: req.user._id,
'userListings._id': req.user.userListings[i]._id
}, {
'userListings.isRead': args.isRead,
'userListings.isFavorite': args.isFavorite,
'userListings.isArchived': args.isArchived
}, function(err, user) {
res.send(user);
});
}
}
Here are the schemas:
var userListingSchema = new mongoose.Schema({
listingId: ObjectId,
isRead: {
type: Boolean,
default: true
},
isFavorite: {
type: Boolean,
default: false
},
isArchived: {
type: Boolean,
default: false
}
});
var userSchema = new mongoose.Schema({
userListings: [userListingSchema]
});
This find also doesn't work, which is probably the first issue:
User.find({
'_id': req.user._id,
'userListings._id': req.user.userListings[i]._id
}, function(err, user) {
console.log(err ? err : user);
});
which returns:
{ stack: [Getter/Setter],
arguments: [ 'path', undefined ],
type: 'non_object_property_call',
message: [Getter/Setter] }
That should be the equivalent of this mongo client call:
db.users.find({'userListings._id': ObjectId("4e44850101fde3a3f3000002"), _id: ObjectId("4e4483912bb87f8ef2000212")})
Running:
mongoose v1.8.1
mongoose-auth v0.0.11
node v0.4.10
when you already have the user, you can just do something like this:
var listing = req.user.userListings.id(req.params.listingId);
listing.isRead = args.isRead;
listing.isFavorite = args.isFavorite;
listing.isArchived = args.isArchived;
req.user.save(function (err) {
// ...
});
as found here: http://mongoosejs.com/docs/subdocs.html
Finding a sub-document
Each document has an _id. DocumentArrays have a special id method for looking up a document by its _id.
var doc = parent.children.id(id);
* * warning * *
as #zach pointed out, you have to declare the sub-document's schema before the actual document 's schema to be able to use the id() method.
Is this just a mismatch on variables names?
You have user.userListings[i].listingId in the for loop but user.userListings[i]._id in the find.
Are you looking for listingId or _id?
You have to save the parent object, and markModified the nested document.
That´s the way we do it
exports.update = function(req, res) {
if(req.body._id) { delete req.body._id; }
Profile.findById(req.params.id, function (err, profile) {
if (err) { return handleError(res, err); }
if(!profile) { return res.send(404); }
var updated = _.merge(profile, req.body);
updated.markModified('NestedObj');
updated.save(function (err) {
if (err) { return handleError(res, err); }
return res.json(200, profile);
});
});
};