mongoose: Finding an id that is nested? - node.js

I'm sorry if this is a repetitive question, but I'm yet to find a solution after scouring the internet for hours :( . I have a database for storing tasks ( a todo list app ) and then I have another so you can make custom todo list with different todos ect...
this database stores the name and an array of the items of the other schema with the specific todos.
const listSchema = {
name: String,
items: [itemSchema]
}
the database looks like this:
[
{
_id: 5ea083694f1d7d0db432b939,
name: 'home',
items: [ [Object], [Object] ],
__v: 2
}
]
and in the items array it looks like this:
[
{ _id: 5ea083764f1d7d0db432b93a, name: 'some todo', check: false },
{
_id: 5ea08391ab923b36780429ee,
name: 'some other todo',
check: false
}
]
What I want to do is find and update by a specific id in the items array, I have the variables
const id = req.body.check // in a for loop and gets id of the specific item
const titleName = req.body.titleName; // gets the title name of the parent object of the items array
and it needs to find using the titleName and the id so that I can update the "check: false" so that it's "check: true"
.
.
.
Found Solution:
List.updateOne({"items._id" : id}, {"$set": {"items.$.check": true}})

You can use findOneAndUpdate(filter, update) of mongoose
const id = req.body.id;
const titleName = req.body.titleName;
collectionName.findOneAndUpdate({"items.id": id, "name": titleName}, {"check": true})

Related

mongodb, check if the key exist while appending to an array

Note: checking if the key books exist or not, creating if not and than updating it.
I am using mongodb driver with nodejs.
In the db.collection('userData')The document looks like this:
{
user_id: 'user1',
books: [{
id: 'book1',
title: 'this is book1'
},
{
id: 'book1',
title: 'this is book1'
}]
}
when inserting a new book entry, how to check if the array of books exists in the document, if not then add a key books in the document and then insert the book entry.
You have to do 2 separate queries,
Find user document
Check condition if books field present
If Present then push object, else set new field
var user_id = "user1";
var bookData = { id: 'book1', title: 'this is book1' };
// FIND USER DATA
var userData = await db.collection('userData').findOne({ user_id: user_id }, { books: 1 });
var updateBody = { $push: { books: bookData } };
// IF BOOKS FIELD NOT PRESENT THEN SET NEW
if (!userData.books) {
updateBody = { $set: { books: [bookData] } };
}
var updateData = await db.collection('userData').updateOne({ user_id: user_id }, updateBody);
console.log(updateData);
Second option you can use update with aggregation pipeline starting from MongoDB 4.2,
$ifNull check is field is null then return []
$concatArrays to concat current books with new book object
var bookData = { id: 'book1', title: 'this is book1' };
db.collection('userData').update({
// put your condition
},
[{
$set: {
books: {
$concatArrays: [
{ $ifNull: ["$books", []] },
[bookData]
]
}
}
}],
{ multi: true }
);
Playground

Search and lookup arrays nested in multiple objects

I find all websites that match the IDs I have in my array and in the below case it is two websites. I then want to look inside each of the conversations arrays of those websites and search a different collection called conversations for conversations that match those IDs. I then want to grab some/all of the fields from those conversations and add it to the JSON document I already have, grouping them by conversation within each website. I've been playing around with aggregate, unwind, and group but can't seem to get anything to work.
router.post('/staffLoadUpTwo', async (req, res) => {
var userId = '5e8f964a9c2d0780c0163825';
const company = await Company.findOne({ 'roles.admins': userId });
var unwrappedCompany = JSON.parse(JSON.stringify(company));
console.log(unwrappedCompany.websites);
const website = await Website.find({
_id: { $in: unwrappedCompany.websites },
});
// const unwindTest = await Website.aggregate([{$unwind : "$conversations"}]);
console.log(website);
});
console.log(website);
[ { conversations: [ '5e90d9ceb089812c9ba1a67b', '5e8f5a6a2582bf629998c3fd' ],
_id: 5e949cc02483c0c0056a1a98,
domain: 'http://x.com',
__v: 0 },
{ conversations: [ '5e8e23595ce6d611cec5033f', '5e8e3afee8e95e1ff94650d3' ],
_id: 5e949ce8f53450c0341b36cd,
domain: 'http://y.com',
__v: 0 } ]
ideal output
[{
_id: "5e949cc02483c0c0056a1a98",
domain: 'http://x.com'
conversations: [
{conversationId: "5e90d9ceb089812c9ba1a67b", messages: {messageArray: ['a'], timeSent: 2}},
{conversationId: "5e8f5a6a2582bf629998c3fd", messages: {messageArray: ['b'], timeSent: 6}}
]
}
_id: "5e949ce8f53450c0341b36cd",
domain: 'http://y.com'
conversations: [
{conversationId: "5e8e23595ce6d611cec5033f", messages: {messageArray: ['c'], timeSent: 1}},
{conversationId: "5e8e3afee8e95e1ff94650d3", messages: {messageArray: ['d'], timeSent: 8}}
]
}]
You need not stress yourself with MongoDB aggregation. Since you're using Mongoose, you can easily use mongoose populate to achieve the result you described in the question.
Provided you've defined the website scheme to be something like this:
const websiteSchema = {
// ...schema definition for other properties
/* Note the ref property used below, the value must be the name of the
conversation model, i.e the stringValue you passed into
mongoose.model(<stringValue>, conversationSchema); */
conversations: [ { type: mongoose.Types.ObjectId, ref: 'Conversations' } ]
}
A mongoose query like this:
const website = await Website.find({
_id: { $in: unwrappedCompany.websites },
}).populate('conversations');
will output an array of website documents whose conversations field are populated i.e, you get the actual conversation document and not just their _ids.
More about Mongoose populate here

Can't push items in mongo array

I can't push items into MongoDB array every time that i try to push a new element it creates an empty object and i cant figure out why,
I already used the
Collection.Array.push({element})&
Collection.save()
but i cant figure out a solution
This is My Schema
const Schema = mongoose.Schema;
var ParticipantSchema = new Schema({
nom:{Type:String},
prenom:{Type:String},
email:{Type:String}
})
var CompetitionSchema = new Schema({
nom:String,
date:Date,
place:String,
participant :[ParticipantSchema]
})
module.exports = mongoose.model("Competition",CompetitionSchema);
This is my funtion
exports.addParticipant=function(req,res){
var newParticipant={
"nom":req.body.nom,
"prenom":req.body.prenom,
"email":req.body.email
}
Competition.updateOne(
{ _id:req.body.id},
{ $push: { participant: newParticipant } },
(err,done)=>{
return res.json(done)
}
);
}
the result is always an empty object like below
{
"_id": "5ded0eeb85daa100dc5e57bf",
"nom": "Final",
"date": "2019-01-01T23:00:00.000Z",
"place": "Sousse",
"participant": [
{
"_id": "5ded0eeb85daa100dc5e57c0"
},
{
"_id": "5dee3c1b08474e27ac70672e"
}
],
"__v": 0
}
There is no problem in your code, the only problem is that in schema definition you have Type, but it must be type.
If you update your ParticipantSchema like this, it will work:
var ParticipantSchema = new Schema({
nom: { type: String },
prenom: { type: String },
email: { type: String }
});
You are using another Schema in the Array. This results in so-called subdocuments (https://mongoosejs.com/docs/subdocs.html). Mongoose does not populate subdocuments by default. So all you see is just the _id. You can use the populate method to see all subdocuments in detail. ( https://mongoosejs.com/docs/populate.html ) .
Example :
Competition.
find({}).
populate('participant').
exec(function (err, comps) {
//
});
You can either use populate on the Model or on the Document. For populating a document, take a look at https://mongoosejs.com/docs/api.html#document_Document-populate . There is also a auto-populate plugin available via npm but in most cases it's not necessary : https://www.npmjs.com/package/mongoose-autopopulate .

Mongoose update or insert many documents

I'm trying to use the latest version of mongoose to insert an array of objects, or update if a corresponding product id already exists. I can't for the life of me figure out the right method to use (bulkWrite, updateMany etc) and I can't can't seem to figure out the syntax without getting errors. For example, i'm trying
Product.update({}, products, { upsert : true, multi : true }, (err, docs) => console.log(docs))
this throws the error DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead. MongoError: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}
and using updateMany just gives me the $set error. I can't seem to find any examples of people using this method, just wondering if somebody can provide me with an example of how to properly use this.
To be clear, I generate an array of objects, then I want to insert them into my db, or update the entry if it already exists. The field I want to match on is called pid. Any tips or advice would be greatly appreciated!
EDIT:
My product schema
const productSchema = new mongoose.Schema({
title : String,
image : String,
price_was : Number,
price_current : {
dollars : String,
cents : String
},
price_save_percent : String,
price_save_dollars : String,
price_save_endtime : String,
retailer : String
})
const Product = mongoose.model('Product', productSchema)
an example of the products array being passed
[
{
title: 'SOME PRODUCT',
image: '',
price_was: '139.99',
price_current: { dollars: '123', cents: '.49' },
price_save_percent: '12%',
price_save_dollars: '16.50',
price_save_endtime: null,
pid: 'VB78237321',
url: ''
},
{ ... },
{ ... }
]
You basically need bulkWrite operation
The array you want to update with
const products = [
{
title: 'SOME PRODUCT',
image: '',
price_was: '139.99',
price_current: { dollars: '123', cents: '.49' },
price_save_percent: '12%',
price_save_dollars: '16.50',
price_save_endtime: null,
pid: 'VB78237321',
url: ''
}
]
The query for bulk update
Model.bulkWrite(
products.map((product) =>
({
updateOne: {
filter: { retailer : product.pid },
update: { $set: product },
upsert: true
}
})
)
)

Mongoose how to auto add _id to objects in array within collection item?

i have a mongo collection that looks like this:
{
name: string
_id: (auto set)
items: array[
name: string
url: string
items: array[
{
name: string,
url: string,
items: []
}
]
]
}
I'm using findByIdAndUpdate (with mongoose) to add an item into the items array:
Menu.findByIdAndUpdate(
req.body.parentid,
{
$push: {
items: {
name: req.body.item.name,
url: req.body.item.url,
items: []
}
}
},
{
safe: true,
upsert: true,
new: true
},
function(err, model) {
if (err !== null) {
console.log(err);
}
}
);
This works fine, but it does not add an _id to each object inserted into the items array. And i really need an id for each one.
I'm guessing it comes from the method used, findByIdAndUpdate as it looks more like an update rather than an insert. If my thinking is correct.
Using mongodb 3.2.10 and mongoose 4.7.6.
Any help would be really appreciated.
Thanks.
EDIT: the _id: (auto set) is not real, it's being automatically added via mongo. But just at the top level objects.
Found the solution in this thread: mongoDB : Creating An ObjectId For Each New Child Added To The Array Field
basically, added
var ObjectID = require('mongodb').ObjectID;
and then forcing the creation:
$push: {
items: {
_id: new ObjectID(),
name: req.body.item.name,
url: req.body.item.url,
items: []
}
}
You dont need to sepcify _id: (auto set) in mongoose schema it will automatically add unique _id with each document.
if you don't define _id in Schema, mongoose automatically add a _id to array item.
for example:
const countrySchema = new Schema({
name: {
type: String
},
cities: [
{
// don't define _id here.
name: String
}
],
});
now when you insert a row, the result is something like this:
{name : 'Iran', cities : [{_id : 6202902b45f0d858ac141537,name :
'Tabriz'}]}

Resources