Mongoose join same collection without reference - node.js

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"
}]
}]

Related

How do i copy a value from a nested array of object to another column in jsonb postgres

How do i a write a query that will copy the phoneNumber from data.accounts.contact.phone.phoneNumber to data.phoneNumber in jsonb postgres.
I tried this command
UPDATE customer."user" SET domain = jsonb_set(domain,'{phoneNumber}', text('domain' ->'accounts'-> 0 -> 'contacts' -> 'phone' -> 'phoneNumber'))
but got this error
Error: HINT: Could not choose a best candidate operator. You might need to add explicit type casts.
SQL state: 42725
Character: 1913
{
"id": "87b31b1f-5dae-4506-8099-9812fa1633eb",
"gender": "F",
"status": "VERIFIED",
"lastName": "Lawal",
"password": "T3m1t0p3",
"username": "aminat2#gmail.com",
"firstName": "Aminat",
"phoneNumber": "",
"accounts": [
{
"status": "IN_REVIEW",
"contact": {
"phone": { "phoneNumber": "7809284029", "diallingCode": "+44" },
"address": { "city": "London", "address": "42 Sark Walk", "country": "United Kingdom", "postcode": "E163PS" },
"emailAddress": "aminat2#gmail.com"
},
"location": {
"id": "4a110b1f-9319-4282-b645-81ea71b53e04",
"status": "ACTIVE",
"currency": {
"id": "1",
"to": false,
"date": "2021-09-19T16:45:33",
"from": true,
"buyFxRate": "1",
"sellFxRate": "1",
"
},
"diallingCode": "+44",
"locationLabel": "United Kingdom",
"
"modifiedDateTime": "2021-09-19T16:45:33",
},
}
],
}

Joining only matching elements

I want to join more than two collections in MongoDB . Is it possible to join?
I have 2 collections and I want to join them based by their ids
news collection
{
"lang": "ru",
"news": [{
"title": "title",
"content": "desc",
"image": "path",
"categoryId": {
"$oid": "6171499e0ba3d75082823c9a"
},
"createdAt": {
"$date": "2021-10-21T12:30:33.215Z"
},
"_id": {
"$oid": "61715d7990905adefcf314d1"
}
}]
}
and I have got newscategory collection like that
[
{
"lang": "ru",
"categories": [{
"name": "sdfsdsdcsdf",
"_id": {
"$oid": "6171499e0ba3d75082823c9a"
}
}]
},
{
"lang": "en",
"categories": [{
"name": "ooo",
"_id": {
"$oid": "61712980f8ee795c6a569dcb"
}
}, {
"name": "yuy",
"_id": {
"$oid": "61712980f8ee795c6a569dcc"
}
}, {
"name": "rtyrty",
"_id": {
"$oid": "61712980f8ee795c6a569dcd"
}
}]
}
]
Expected Output:
[
{
"_id": "61715d7990905adefcf314d0",
"lang": "ru",
"news": [
{
"title": "sdf",
"content": "sdf",
"image": "asdas",
"categoryId": "6171499e0ba3d75082823c9a",
"caregory": [
{
"name": "sdfsdsdcsdf",
"_id": "6171499e0ba3d75082823c9a"
},
]
"createdAt": "2021-10-21T12:30:33.215Z",
"_id": "61715d7990905adefcf314d1"
}
],
Match is based on both "category_id" and "categories._id".
How can I achieve result like that ?

MongoDB merge two same schema collection

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"},

Query to retrieve every subdocument alone without passing parent id using Mongoose

Here i like to explain my problem.
How can i write a mongoose query to retrieve every subdocument from JSON without passing parent_id.
[
{
"_id": "56a320003fe17cc7363dd0d7",
"name": "Leanna Jacobson",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Riley Case"
},
{
"id": 1,
"name": "Herman Carter"
},
{
"id": 2,
"name": "Pacheco Woodard"
}
]
},
{
"_id": "56a3200001501cfa1ea2641d",
"name": "Juliana Bonner",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Keller Woodward"
},
{
"id": 1,
"name": "Fern Knight"
},
{
"id": 2,
"name": "Cain Richards"
}
]
},
{
"_id": "56a3200006864c78ecb1aeed",
"name": "Gena Stark",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Kate Franco"
},
{
"id": 1,
"name": "Araceli Mcclure"
},
{
"id": 2,
"name": "Molly Nelson"
}
]
},
{
"_id": "56a320006d868155161038b6",
"name": "Eve Gonzalez",
"gender": "female",
"friends": [
{
"id": 0,
"name": "Pam Lang"
},
{
"id": 1,
"name": "Christy Marks"
},
{
"id": 2,
"name": "Donovan Warren"
}
]
},
{
"_id": "56a3200066b94852f5680568",
"name": "Coleman Wooten",
"gender": "male",
"friends": [
{
"id": 0,
"name": "Roberta Olson"
},
{
"id": 1,
"name": "Roseann Reid"
},
{
"id": 2,
"name": "Kerri Russell"
}
]
}
]
Here i need to retrieve every friends details from the subdocument array friends for every parent.
so how can i write query for this?????
Suppose the name of your schema is Person, try this one.
//find all document, only select `friends` field from every document
Person.find({}, 'friends', function (err, friends) {
// the return friends is the [[friends], [friends], ...]
});

Mongoose find anywhere inside document

i need to find record matching userId. the userId can be in any level of document.
It can be in parent level or it can be inside friends array.
[{
"_id": "543357620c9af6066e689713",
"createdDate": "2014-10-06 08:00 pm",
"cancle": false,
"eventDate": "2014/12/12",
"eventstatus": true,
"location": "chennai",
"userId": "54334def7e85de48638d1069",
"createdBy": "four",
"eventName": "jamba",
"__v": 0,
"friends": []
},
{
"_id": "543356fe0c9af6066e68970c",
"createdDate": "2014-10-06 07:59 pm",
"cancle": false,
"eventDate": "2014/12/12",
"eventstatus": true,
"location": "chennai",
"userId": "54310801e2659ecc3650100b",
"createdBy": "one",
"eventName": "tea ",
"__v": 0,
"friends": [
{
"userId": "54310814e2659ecc3650100c",
"userName": "two",
"phoneNumber": "22222222"
},
{
"userId": "54310945e2659ecc3650100d",
"userName": "three",
"phoneNumber": "33333333"
},
{
"userId": "54334def7e85de48638d1069",
"userName": "four",
"phoneNumber": "44444444"
}
]
}]
i am trying for long time. Any help is highly appreciated.
You need the $or operator. Also, you don't need to iterate to query for elements within an array. Assuming your collection is named Events:
Events.find({
$or: [
{ _id: req.params.userId },
{ 'friends.userId': req.params.userId }
]
}).exec();

Resources