Adding element inside nested array in mongoose - javascript

Server Started at Port 3000...
{
_id: new ObjectId("61c707e9f4ff040a47d27c3f"),
username: 'adityaaryam',
password: '1234',
nameOfUser: 'Aditya Aryam',
emailOfUser: 'adityaaryam#gmail.com',
userAllLists: [
{
name: 'Hello',
items: [],
_id: new ObjectId("61c70d915448262d1dca1a69")
},
{
name: 'Work',
items: [],
_id: new ObjectId("61c70d965448262d1dca1a70")
},
{
name: 'Home Work',
items: [],
_id: new ObjectId("61c70d9b5448262d1dca1a79")
},
{
name: 'Hello',
items: [],
_id: new ObjectId("61c70e7f5448262d1dca1a84")
},
{
name: 'Play',
items: [],
_id: new ObjectId("61c7126a5448262d1dca1a9b")
},
{
name: 'Eat',
items: [],
_id: new ObjectId("61c71325b0219e6ce4f57990")
},
{
name: 'Walla',
items: [],
_id: new ObjectId("61c7197de9564390d506cbe9")
}
],
__v: 7
}
This is how my database looks like. I want to push new elements to "items" array which is nested inside the "userAllLists" array using mongoose. How do I implement this?
I have been trying findOneAndUpdate using $push but I am not able to achieve my desriable results.
My Schemas are as follows:
const itemSchema = {
name: String
};
const customListSchema ={
name:String,
items:[itemSchema]
};
const userSchema={
username: String,
password: String,
nameOfUser: String,
emailOfUser: String,
userAllLists: [customListSchema],
};
Thanks in Advance!

I think $push is the right way to push new elements to nested arrays, you didn't show the code you tried to see if it works or not, at all here is an example based on your schema
User.update({_id: "61c707e9f4ff040a47d27c3f", }, {
'$push': {
"userAllLists.$[].items": {name: "test item name"}
}
});
Note: $[] expressions will push the specified object inside all items arrays that exist in userAllLists
To push the item for only specific userAllLists object you can use the following syntax
User.update({_id: "61c707e9f4ff040a47d27c3f", "usersAllLists._id": "61c70d915448262d1dca1a69"}, {
'$push': {
"userAllLists.$.items": {name: "test item name"}
}
});
this will ensure to push the item object to the specified usersAllLists object which has this id 61c70d915448262d1dca1a69

Related

Node Js how to fetch data from database in an hierarchical way

I'm writing a back code using NodeJs to fetch some data from backend, I want dataBase data to be like this
like this:
data = [{
name: "Admin",
id: '1',
children: [
{ name: "Admin", id: "1" },
{ name: "groupe1", id: "2" },
{
name: "groupe2", id: "1455", children: [
{ name: "groupe2", id: "1455" },
{ name: "gro", id: "5444" },
{ name: "hhrr", id: "45" }
]
}
]
}]
the idea is simple we have a list of group each group has a parent I want to display all the groups list in an hierarchical way the top one of the tree is done
Some groups are parents and groups in the same time and some others are only groups if the group is not parent we add an object with its name and ID in the array of children of his parent
if this groups is a parent that's mean it has children we add an object with its ID and name in the array of children of his parents, and we add property children for the object which is array named children with for the first time an object with the name and the id of the group etc...
i tryed to do this but it did not work
const getParentsByType = async ({ name, _id }) => {
let parentResult = [
{
id: _id,
name: name,
children: [
{
id: _id,
name: name,
},
],
},
];
parentResult= await findParent(_id, parentResult[0].children, 0);
return parentResult;
};
const findParent = async (parentId, parentResult, itemPos) => {
let children = await Models.GroupModel.find({ parent: parentId, status: true }).select('name _id');
for (let i = 0; i < children.length; i++) {
let childrenList = await Models.GroupModel.find({ parent: children[i]._id, status: true }).select('name _id');
if (childrenList.length != 0) {
parentResult.push(buildParentWithChild(children[i]._id, children[i].name));
findParent(children[i]._id,parentResult.children[i],itemPos++)
} else {
parentResult.push(buildParent(children[i]._id, children[i].name));
}
}
return parentResult
};
and this the model of the data base
const Group = mongoose.Schema({
name: {
type: String,
required: true,
},
status: {
type: Boolean,
required: true,
},
parent: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Group',
},
});
i had two days trying to resolve tis but with no result
i need some helps and Thank you
Try parsing your returned data. It validates your data as objects i dont see any problem with your function regardless i still have no idea what format your a trying to build.
let children = JSON.parse(JSON.stringify(await Models.GroupModel.find({ parent: parentId, status: true }).select('name _id')));
let childrenList = JSON.parse(JSON.stringify(await Models.GroupModel.find({ parent: children[i]._id, status: true }).select('name _id')));
If I understand you right, you want to convert the array returned by Models.GroupModel.find, and which looks like
var dbresult = [
{_id: "1", parent: null, name: "one"},
{_id: "2", parent: "1", name: "two"}
];
into a hierarchical structure. This can be done with a function that adds all children of a given parent p, including, recursively, their children. Like the following:
function children(p) {
var result = [];
for (r of dbresult) if (r.parent === p) {
var row = {_id: r._id, name: r.name};
var chld = children(r._id);
if (chld.length > 0) row.children = chld;
result.push(row);
}
return result;
}
console.log(JSON.stringify(children(null)));
Note that this approach requires only one database access (to fill the dbresult) and is therefore probably faster than your findParent function.

Push new Object inside a document array

I have this schema for my user
const userSchema = mongoose.Schema({
firstName: {
type: String,
},
notifications : [{
description: String,
status: {
type: String,
enum : ['read', 'unread'],
default: 'unread'
},
dateAdded:{
type: Date,
default: Date.now
}
}],
})
supposedly I want to find the user _id first then insert a new object inside the new notification array. and it should look like this
{
_id: ObjectId('123')
firstName: 'John Doe'
notifications:[
{
description: 'somedescription'
status: 'unread'
},
{
description: 'somedescription2'
status: 'unread'
}
]
}
How can I achieve this, assuming that the notification property is non existent in the user document first, i need to check if notification property is present else add the notification property and push the new object
User.updateOne(
{ _id: userId },
{ $push: { notifications: {description: 'new notifications'} } }
)
this code is not working for me
Use $addToSet operator to achieve that
User.updateOne(
{ _id: userId },
{ $addToSet: { notifications: {description: 'new notifications'} } }
)
If that doesn't work try to add the default value too, and then that must work
User.updateOne(
{ _id: userId },
{ $addToSet: { notifications: {description: 'new notifications',
'status': 'unread'} } }
)

Combining result of two different Queries from two different Model MongoDB

So first I have a query that finds books that has been borrowed by user, it will search using the Borrow model
const bookqueryinitial = await Borrow.find({borrower_Id : String(_id), borrowStatus: req.query.status}).sort({"borrowDate": -1 }).skip(skip).limit(pageSize);
it will return results like this
[
{
_id: new ObjectId("628ebcc10944a1223397b057"),
borrower_Id: '6278d1b6b4b7659470572e19',
borrowedbook_Id: '62710ac63ad1bfc6d1703162',
borrowStatus: 'pending',
borrowDate: 2022-05-25T23:33:21.849Z,
__v: 0
},
{
_id: new ObjectId("628d9c0b9a3dc72f4aa72f1a"),
borrower_Id: '6278d1b6b4b7659470572e19',
borrowedbook_Id: '62710ac63ad1bfc6d170314d',
borrowStatus: 'pending',
borrowDate: 2022-05-25T03:01:31.416Z,
__v: 0
}
]
next is I will map through the borrowedbook_Ids of the result and store them in an array
const booksinsidequery = bookqueryinitial.map(bookids=>{
return bookids.borrowedbook_Id
})
then I will search the ids that is stored in array and search for those ids in the Book model
const bookquery = await Book.find({ '_id': { $in: booksinsidequery } });
\\and the result is somethign like this
[
{
_id: new ObjectId("62710ac63ad1bfc6d170314d"),
title: "Girl who kicked the Hornet's Nest",
author: 'Larsson, Steig',
genre: [ 'fiction' ],
publisher: '',
dateOfPublication: 2017-10-25T00:00:00.000Z,
noOfCopies: 14,
type: 'Article',
form: 'Fiction',
isbn: '978-69793-4824559-56755-9',
dateAdded: 2003-04-23T00:00:00.000Z,
noOfBookmarks: [ [Object] ],
noOfLikes: [],
},
{
_id: new ObjectId("62710ac63ad1bfc6d1703162"),
title: 'We the Nation',
author: 'Palkhivala',
genre: [ 'philosophy' ],
publisher: '',
dateOfPublication: 2011-11-22T00:00:00.000Z,
noOfCopies: 94,
type: 'Book',
form: 'Non-fiction',
isbn: '978-65685-4156343-802140-8',
dateAdded: 2010-06-08T00:00:00.000Z,
noOfLikes: [],
noOfBookmarks: []
}
]
Now before sending the result of the query to the client side, I want to bind my initial queries from Borrow model to my Book model and the final result should be like this
[
{
_id: new ObjectId("62710ac63ad1bfc6d170314d"),
title: "Girl who kicked the Hornet's Nest",
author: 'Larsson, Steig',
genre: [ 'fiction' ],
publisher: '',
dateOfPublication: 2017-10-25T00:00:00.000Z,
noOfCopies: 14,
type: 'Article',
form: 'Fiction',
isbn: '978-69793-4824559-56755-9',
dateAdded: 2003-04-23T00:00:00.000Z,
noOfBookmarks: [ [Object] ],
noOfLikes: [],
//added properties based on matched condition (Borrow.borrowedbook_Id === Book._id)
borrowStatus: 'pending',
borrowDate: 2022-05-25T03:01:31.416Z,
},
{
_id: new ObjectId("62710ac63ad1bfc6d1703162"),
title: 'We the Nation',
author: 'Palkhivala',
genre: [ 'philosophy' ],
publisher: '',
dateOfPublication: 2011-11-22T00:00:00.000Z,
noOfCopies: 94,
type: 'Book',
form: 'Non-fiction',
isbn: '978-65685-4156343-802140-8',
dateAdded: 2010-06-08T00:00:00.000Z,
noOfLikes: [],
noOfBookmarks: [],
//added properties based on matched condition (Borrow.borrowedbook_Id === Book._id)
borrowStatus: 'pending',
borrowDate: 2022-05-25T23:33:21.849Z,
}
]
How can I attain these results?
You can achieve this with the aggregation framework that MongoDB provides.
Based on your data here is an example: https://mongoplayground.net/p/DQJIbcqBDKM
Lookup : the operator helps you to join data between different collections and the matched data will be in an array. In this case, called borrows
Unwind : will create a document per item on a specified array
Addfields : Allows you to create new attributes, in this case, the two that you wanted "borrowStatus" and "borrowDate"
Project : this operator allows you to hide or show data in the next stage. In this case, 0 means that we will hide a specific attribute

Filter array of objects to a single matching object in mongodb/meteor

I have a query which looks as so:
var writer = Writers.findOne({
_id: writerId,
books: {
$elemMatch: {
id: Books.findOne({ slug: bookSlug })._id
}
}
});
However, this will return the full list of classes in the query.
{
name: "H.P. Lovecraft",
books: [{
id: "1234",
slug: "at-the-mountains-of-madness"
}, {
id: "5678",
slug: "herbert-west-reanimator"
}]
}
Would there be a way to eliminate all information except for the one item in the list I want and make it into an object? That is to say, I want my final result to be:
{
name: "H.P Lovecraft",
book: {
id: "1234",
slug: "herbert-west-reanimator"
}
}
How would this be done in Meteor with mongodb?
One approach you could take is to use the $elemMatch projection operator with the findOne() query. For the document with _id equal to writerId, the $elemMatch projection returns only the first matching element from the array:
var bookId = Books.findOne({ slug: bookSlug })._id,
writer = Writers.findOne({ _id: writerId },
{ books: { $elemMatch: { id: bookId } },
_id: 0,
name: 1
}
);
Another approach would be to use Underscore library's _.find() method to return the specific array element:
var bookId = Books.findOne({ slug: bookSlug })._id,
writer = Writers.findOne({
_id: writerId,
books: {
$elemMatch: {
id: bookId
}
}
}),
book = _.find(writer.books, function(book) {return book.id === bookId});

Saving Mongoose documents with empty sub-documents collections results in duplicate key error

I have two mongoose schemas:
var productSchema = new Schema({
name: { type: String, required: true, unique: true },
...
});
...
var categorySchema = new Schema({
...
products: [ProductSchema]
});
When I try to save categories
var categories = [
{..., products: []},
{..., products: []}
];
or even without products
var categories = [
{...},
{...}
];
I'm getting error
{ [MongoError: E11000 duplicate key error index: test.categories.$products.name_1 dup key: { : undefined }]
name: 'MongoError',
err: 'E11000 duplicate key error index: test.categories.$products.name_1 dup key: { : undefined }',
code: 11000,
n: 0,
lastOp: { _bsontype: 'Timestamp', low_: 6, high_: 1404282198 },
ok: 1 }
It seems like mongoose is trying to save products with undefind names.
Mongoose log before getting error:
Mongoose: categories.insert({ __v: 0, products: [], _id: ObjectId("53b3c167d28a86102dec420a"), order: 1, description: 'Category 1', name: 'Cat 1' }) {}
Mongoose: categories.insert({ __v: 0, products: [], _id: ObjectId("53b3c167d28a86102dec420b"), order: 2, description: 'Category 2', name: 'Cat 2' }) {}
If I remove unique: true from the name property of productSchema two categories are added with empty products [] collections.
What am I doing wrong?
Thank you!
This is pretty normal really. The empty array is essentially considered to be a "null" value for the "products.name" field, that of course violates the unique constraint on the index.
You could essentially "skip" any values of "name" that are in fact undefined, and you do this by adding the "sparse" property to the index. In the present schema path form:
var productSchema = new Schema({
name: { type: String, required: true, unique: true, sparse: true }
});
var categorySchema = new Schema({
products: [productSchema]
});
Now as long as there is no value in "name" there will be no problem unless of course it actually exists somewhere. Make sure to drop the index already created first though.
Just a note, be aware that what this is doing is making sure that the "products.name" values are unique for the "whole" collection. If you are just trying to make sure they are unique for a given category, then indexing is not your solution and you need to ensure that by other means.

Categories

Resources