I want to read data in tree structure with nested child of one parent, I am using mongodb. enter image description here
I want this type of expected data from the mongodb
[
{
"_id": "5d1fd59b1d26b42b2fdecf69",
"email": "admin#admin",
"userName": "admin",
"parrentId": "5d1fd9a6f612542b8d72337d",
"childs": [
{
"_id": "5d1fd61af612542b8d723376",
"email": "test1#test.com",
"userName": "test1",
"parrentId": "5d1fd59b1d26b42b2fdecf69",
"childs": [
{
"_id": "5d1fd91af612542b8d723379",
"email": "test2#test.com",
"userName": "test2",
"parrentId": "5d1fd59b1d26b42b2fdecf69",
"childs": []
},
{
"_id": "5d1fd91af612542b8d723379",
"email": "test2#test.com",
"userName": "test2",
"parrentId": "5d1fd59b1d26b42b2fdecf69",
"childs": []
},
]
}
]
}
]
Related
I need to join a same collection without any reference using mongoose.
What i get is:
[{
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test",
"email": "test#gmail.com"
},
{
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test1",
"email": "test1#gmail.com"
}, {
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test2",
"email": "test2#gmail.com"
}, {
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test3",
"email": "test2#gmail.com"
}
]
What i need is like this
[{
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test",
"email": "test#gmail.com",
"users": [{
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test1",
"email": "test1#gmail.com"
}, {
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test2",
"email": "test2#gmail.com"
}, {
"_id": "57b188d67aa27ae4ee87e11c",
"name": "test3",
"email": "test2#gmail.com"
}]
}]
There is a sub-field called 'name' in MongoDB Collection (User):
[
{
"teacher": {
"name": "Alex",
"email": "alex#domain.com"
},
"waiter": {
"name": "Feliks",
"email": "feliks#domain.com"
},
"pilot": [
{
"name": "Sam",
"email": "sam#domain.com"
},
{
"name": "alice",
"email": "alice#domain.com"
}
],
},
{
"teacher": {
"name": "max",
"email": "max#domain.com"
},
"waiter": {
"name": "Sam",
"email": "sam#domain.com"
},
"pilot": [
{
"name": "richard",
"email": "richard#domain.com"
},
{
"name": "alice",
"email": "alice#domain.com"
}
],
}
]
How can I find data based on the field 'name'. For example, when I'm looking for 'Sam', it should return me the all documents since 'Sam' is in "waiter" and "pilot" in first and second documents respectively.
I cannot do something like:
User.find({"teacher.name": "Sam", "waiter.name": "Sam", "pilot.name": "Sam" })
This will return me nothing as it is an AND logic. What I need is an OR logic.
You can use the $or operator.
So the query should look like this:
User.find({ $or: [
{ "teacher.name": "Sam" },
{ "waiter.name": "Sam" },
{ "pilot.name": "Sam" }
]
});
Read here for me details.
I have the following mongoose collection
{
"_id": "5f494ca2d84e5d2ae800d5a4",
"email": "test#gmail.com",
"customer": [
{
"entries": [],
"payment": [],
"_id": "5f4950e7f4e0162c2c8af05f",
"name": "demo",
"mobile": 8877887788
},
{
"entries": [],
"payment": [],
"_id": "5f49514230dc8e3f0063f5e1",
"name": "demo2",
"mobile": 8877887788
},
{
"entries": [],
"payment": [],
"_id": "5f4956d92d83c03e68905fbb",
"name": "demo3",
"mobile": 8877887788
}
],
"createdAt": "2020-08-28T18:27:46.090Z",
"updatedAt": "2020-08-28T19:27:25.882Z",
"__v": 5
}
We have multiple users Above this is only 1 user schema
User have multiple Customers
Goal :-
Verify User -> Select particular Customer -> push entries Values in particular Customer
Find user by Email ID
then We have multiple Customers in this User so I want to find particular 1 Customer and target entries array .. and push value in entries Array
eg:- What i Want
{
"_id": "5f494ca2d84e5d2ae800d5a4",
"email": "test#gmail.com",
"customer": [
{
"entries": [
{ "amount" : 40 ,
"date" : "2020-08-28T19:27:25.882Z"
}
],
"payment": [],
"_id": "5f4950e7f4e0162c2c8af05f",
"name": "demo",
"mobile": 8877887788
},
{
"entries": [],
"payment": [],
"_id": "5f49514230dc8e3f0063f5e1",
"name": "demo2",
"mobile": 8877887788
},
{
"entries": [],
"payment": [],
"_id": "5f4956d92d83c03e68905fbb",
"name": "demo3",
"mobile": 8877887788
}
],
"createdAt": "2020-08-28T18:27:46.090Z",
"updatedAt": "2020-08-28T19:27:25.882Z",
"__v": 5
}
So I would use mongoose find by id to find the user then for each through the customer array, then save your changes.
db.<collectionName>.findById(id).then((collection) => {
collection.customer.forEach((customer, index) => {
if (customer._id === custId) {
collection.customer[index] = { ...collection.customer[index], quantity: newQuantity }
};
});
db.<collectionName>.findByIdAndUpdate(id, collection);
});
Something like that.
how to make this data to asc order by user's first name and user's last name.
I got the response, want to sort the records by user's first name but it is taking from creation date I guess when using sort how can I make it base on user's first name and user's last name please guide
{
"response": {
"items": [
{
"_id": "5e71f86bd300b313df52fb2f",
"last_message": {
"text": "Alex",
"users": [
{
"_id": "5e4a8d2d3952132a08ae5764",
"first_name": "zack",
"last_name": "Write"
}
]
},
"texter": [
"alex",
"$0ctg"
],
"title": "New group1",
"group": true,
"members": [
{
"_id": "5e4a8afc3952132a08ae575e",
"first_name": "test3",
"last_name": "test4"
}
],
"managers": [
"5e4a8afc3952132a08ae575e"
],
"member": {
"_id": "5e4a8afc3952132a08ae575e",
"first_name": "test3",
"last_name": "test4"
}
},
{
"_id": "5e4e740f380054797d9db621",
"last_message": {
"text": "",
"users": [
{
"_id": "5e4a8d2d3952132a08ae5764",
"first_name": "yuhan",
"last_name": "jacob"
}
]
},
"texter": [
"",
"",
"",
"",
"",
"new iphone x\n brand new iphone wv wwvqzwzvq had sqswgscq wow you wholeheartedly q \n $600.00",
"helo",
"hello",
"hi"
],
"members": [
{
"_id": "5e4d0973babf2b74ca868f4d",
"first_name": "alex",
"last_name": "hales"
}
],
"managers": [],
"member": {
"_id": "5e4d0973babf2b74ca868f4d",
"first_name": "alex",
"last_name": "hales"
}
}
]
}
}
Tried
{
$sort: {
users: 1,
}
},
doesn't help much
Also if I would like to add two field asc desc order what would be the process in MongoDB
Try this hope this will help you !
{
$sort: { "users.first_name": 1 }
},
I am using mongoDB with node express and I have two collection in my db having name "user" and "user_archive". I want to merge both collection data in a query and want to perform pagination.
For single collection pagination I am using below code:
user.find(condition)
.select()
.limit(perPage)
.skip(perPage * page)
.sort(sortObject)
.exec(function(err, data)
{
callback(err,data);
});
I want to perform pagination by merging both user and user_archive collection data.
user collection
{"name":A}
{"name": B}
user_archive collection
{"name":C}
{"name":D}
I need to merge both collection like below and then want to perform pagination:
{"name":A}
{"name":B}
{"name":C}
{"name":D}
Could anyone please suggest how can I do that in my code.
You can use aggregation piplines.
For example
User collection
{ "_id": "1", "email": "test1#gmail.com", "fullName": "Jon1"},
{ "_id": "2", "email": "test2#gmail.com", "fullName": "Jon2"},
{ "_id": "3", "email": "test3#gmail.com", "fullName": "Jon3"}
Post Collection
{ "_id": "11", "owner": "1", "content": "text1"},
{ "_id": "12", "owner": "2", "content": "text2"},
{ "_id": "13", "owner": "3", "content": "text3"},
.
async function getData (options) {
let query = [];
// merge posts and users
query.push(
{ $lookup: { from: 'posts', localField: '_id', foreignField: 'owner', as: 'posts' } }
);
// pagination
if (options.page && options.size) {
let skip = options.size * (options.page - 1);
query.push(
{ $skip: skip },
{ $limit: options.size }
);
}
return await db.posts.aggregate(query);
}
The result from this function will be
{ "_id": "11", "owner": { "_id": "1", "email": "test1#gmail.com", "fullName": "Jon1" }, "content": "text1"},
{ "_id": "12", "owner": { "_id": "2", "email": "test2#gmail.com", "fullName": "Jon2" }, "content": "text2"},
{ "_id": "13", "owner": { "_id": "3", "email": "test3#gmail.com", "fullName": "Jon3" }, "content": "text3"}
For example if options = { page: 1, size: 2 }, result will be
{ "_id": "11", "owner": { "_id": "1", "email": "test1#gmail.com", "fullName": "Jon1" }, "content": "text1"},
{ "_id": "12", "owner": { "_id": "2", "email": "test2#gmail.com", "fullName": "Jon2" }, "content": "text2"},