How to use mongodb aggregation to transform document in nested array - node.js

My document looks like as shown below and I want to transform it using aggregation. Inside favourite_products I have product array and it has product_id corresponding to each shops. Now I just want product ids of all shops but sorted based on time. :
"favourite_products": [
{
"shop": {
"shop_id": "59465888f7babb000485574b",
"time": "2017-07-12T06:11:19.817Z"
},
"product": [
{
"product_id": "594c2d56f7afcf00043b1195",
"time": "2017-07-12T06:10:36.775Z"
},
{
"product_id": "594ac36c76de720004e819f6",
"time": "2017-07-12T06:11:19.817Z"
}
]
},
{
"shop": {
"shop_id": "593acc24a2902d0004211f1f",
"time": "2017-07-12T06:12:59.372Z"
},
"product": [
{
"product_id": "594ac36c76de720004e819f6",
"time": "2017-07-12T06:12:59.372Z"
}
]
}
]
I want to transform it into this:
"favourite_products"
["59465888f7babb000485574b",594c2d56f7afcf00043b1195","594ac36c76de720004e819f6","593acc24a2902d0004211f1f","594ac36c76de720004e819f6"]

Below returns time ordered documents of favourite_products.product.product_id
use project if you want the result as different documents.
or use group if you want the result as the array in one document.
db['testing-aggregate'].aggregate([
{$unwind:'$favourite_products'},
{$unwind:'$favourite_products.product'},
{$sort:{'favourite_products.product.time':1}}, // order by time. 1=ascending | -1=descending
// {$project:{
// '_id':0, // exclude _id from output
// 'favourite_products':'$favourite_products.product.product_id' // return only product_id
// }},
{$group:{
_id:null,
product_id:{$push:'$favourite_products.product.product_id'}
}}
])

Related

PouchDB/CouchDB Group By Value in Array

I am using PouchDB and I have a dataset representing a social network in a graph. People are documents, and the people they follow are in an array of the _id of the person followed. Here is a sample of the data:
[
{
"_id": "mc0001",
"name": "Jill Martin",
"joined": "2020-01-15",
"follows": []
},
{
"_id": "mc0002",
"name": "Elena Markova",
"joined": "2020-01-21",
"follows": ["mc0001"]
},
{
"_id": "mc0003",
"name": "Carlos Sanchez",
"joined": "2020-01-27",
"follows": ["mc0001", "mc0002"]
},
{
"_id": "mc0004",
"name": "Ai Sato",
"joined": "2020-02-21",
"follows": ["mc0001", "mc0003"]
},
{
"_id": "mc0005",
"name": "Ming Wu",
"joined": "2020-03-21",
"follows": ["mc0002", "mc0003", "mc0004"]
}
]
What I would like to do is query for each person, and get a list of followers. I am looking for something like this:
[
{
"_id": "mc0001",
"followers": ["mc0002", "mc0003", "mc0004"]
},
{
"_id": "mc0002",
"followers": ["mc0003", "mc0005"]
},
{
"_id": "mc0003",
"followers": ["mc0004", "mc0005"]
},
{
"_id": "mc0004",
"followers": ["mc0005"]
},
{
"_id": "mc0005",
"followers": []
}
]
Is there a way to do this without changing the data structure (e.g. moving the followers array into the doc of the person being followed)?
Create a Map/Reduce view that loops through the follows array in each document and emits those; like this:
function (doc) {
for(var i =0; i<doc.follows.length; i++) {
emit(doc.follows[i], null);
}
}
You end up with an index keyed on a user and where each row has the id of a follower of that user. You can then query the index, supplying the key of the user whose followers you want to find, like this:
$URL/users/_design/users/_view/by-follower?key="mc0001"&reduce=false
You will get something like this:
{"total_rows":8,"offset":0,"rows":[
{"id":"mc0002","key":"mc0001","value":null},
{"id":"mc0003","key":"mc0001","value":null},
{"id":"mc0004","key":"mc0001","value":null}
]}
This is not exactly the format of the data you have in your question, but you can see that the id field in each object contains a follower of your desired user, so you can go from there.

How can I write query in mongodb?

I have a collection of mongodb like this :
[{
"_id":"ObjectId(""51780fb5c9c41825e3e21fc4"")",
"name":"CS 101",
"students":[
{
"name":"raj",
"year":2016
},
{
"name":"rahul",
"year":2017
},
{
"name":"anil",
"year":2018
}
]
},
{
"_id":"ObjectId(""51780fb5c9c41825e3e21fs4"")",
"name":"CS 102",
"students":[
{
"name":"mukesh",
"year":2016
},
{
"name":"mohan",
"year":2017
},
{
"name":"mangal",
"year":2018
}
]
}
]
I've been looking for similar questions like this one: Mongo db - Querying nested array and objects but in that question they're looking for a specific element inside the "messages" object (in my case) for example. Same as in this other question: Query for a field in an object in an array with Mongo? where they're using $mapan d I don't think it fits my needs.
The documents to find have this structure:
[{
"_id":"ObjectId(""51780fb5c9c41825e3e21fc4"")",
"name":"CS 101",
"students":[
"raj","rahul","anil"
]
},
{
"_id":"ObjectId(""51780fb5c9c41825e3e21fs4"")",
"name":"CS 102",
"students":[
"mukesh","mohan","mangal"
]
}
]
how to solve this?
From the question and datasets, you are trying to return students with an array of student's name (string) instead of the array of student object.
Use $project to display students as students.name array.
db.collection.aggregate([
{
$project: {
"_id": "$_id",
"name": "$name",
"students": "$students.name"
}
}
])
Sample Solution 1 on Mongo Playground
OR
Use $set to replace the students field with students.name array.
db.collection.aggregate([
{
$set: {
"students": "$students.name"
}
}
])
Sample Solution 2 on Mongo Playground

Mongodb Update into three level embedded document only on field value

I am trying to update three level Embedded document only one field. i am posting my data below in which I am trying to update only one field into the document. here is my Collection in which i am trying to update View value. {
"_id": "5bbc7614b6160b29f05854c7",
"createdAt": "2018-10-09T09:34:12.604Z"
"subcategories": [
{
"status": "1",
"_id": "5bbc762fb6160b29f05854c8",
"createdAt": "2018-10-09T09:34:39.008Z",
"videos": [
{
"views": 0,
"createdAt": "2018-10-12T11:40:08.752Z",
"_id": "5bc08818ed05cf1c5e01103c",
"user_id": "5ba08df7a68f5f1e43f05983",
"description": "sdf",
"size": "30.76 MB",
"duration": "11:11",
"video": "https://www.gpnext.org"
},
{
"views": 10,
"createdAt": "2018-10-12T11:40:08.752Z",
"_id": "5bc08818ed05cf1c5e01103d",
"user_id": "5ba08df7a68f5f1e43f05984",
"description": "qwerty",
"size": "35.76 MB",
"duration": "10:10",
"video": "https://www.gpnext1.org"
}
]
},
]
}
I am trying to update Views value only but when i am executing bellow mentioned query it is deleting all video data and inserting only views field there. can someone guide me to update Views value without effecting other fields values. here is my query for updating view value.
VideoCategory.findOneAndUpdate(
{'subcategories.videos._id' : mongoose.Types.ObjectId(req.body.video_id)},
{ $set :
{
'subcategories.$.videos': {
'views' : 4
}
}
},
function(err, category){
if (err)
return res.send({ status: false, message: 'error in Increment'});
else
return res.json({ status: true, message: 'View incremented'});
}
);
Positional operator ($) works only for arrays with one-level depth. In your case you need positional filtered operator which is available in MongoDB 3.6 or higher. Try:
db.col.update(
{ _id: "5bbc7614b6160b29f05854c7" },
{ $set: { "subcategories.$[subcategory].videos.$[video].views": 4 }},
{ arrayFilters: [ { "subcategory._id": "5bbc762fb6160b29f05854c8" }, { "video._id": "5bc08818ed05cf1c5e01103c" } ] })
You set json object with only view key, thats why all other data expect "view" are deleted, in this case you need to use positional operator twice, like this
{
$set : {
"subcategories.$.videos.$.views": "4"
}
But MongoDb has limitation on positional operator.
The positional operator can be used only once in a query.
Here is the issue link https://jira.mongodb.org/browse/SERVER-831
I suggest two solution, but highly recommend to use first one
Create another Video Collection, and save video _id(pointer) into your array, than you can easily make any update query you want
Fetch your data, then do some changes, after that update whole data you fetched

Searching many parameters in a Cloudant noSQL DB

I am using a Cloudant database on Bluemix to storage products in a Node.js server. These products will be searched by categories. To find a product that has only one category, would not be a problem because a search is made by comparing the string that is sent as a search parameter with the category string that is saved in the database. The problem occurs when a product has two or more categories. At the time of making the comparison of string to string, it would never coincide.
The products can have as many categories as they need.
Any ideas?
if i am understanding your question correctly, you may want to store category as an array of strings and index each element in the array. you can then search products against a single or multiple categories.
for example, given the following documents:
doc 1
{
"name": "product1",
"category: ["abc"]
}
doc 2
{
"name": "product2",
"category: ["abc", "def"]
}
you can set up a search index similar to:
function (doc) {
if (doc.category) {
for (var i in doc.category) {
index("category", doc.category[i], {"store": true, "facet": true})
}
}
}
then you may run queries like such:
.../{dbname}/_design/{designdocname}/_search/{searchindexname}?q=category:abc
which would return both product1 and product2
or:
.../{dbname}/_design/{designdocname}/_search/{searchindexname}?q=category:abc+AND+category:def
which would return only product2.
additional info: https://developer.ibm.com/clouddataservices/cloudant-search/
You should store one or more categories in array format in Cloudant database in "category" parameter
{
"_id": "producto1",
"category: ["TV", "cameras", "computers"]
}
Then you should create a search index
function (doc) {
if (doc.category) {
for (var i in doc.category) {
index("category", doc.category[i], {"store": true, "facet": true})
}
}
}
Now you can query the documents from Cloudant Query
{
"selector": {
"_id": {
"$gt": 0
},
"category": {
"$all": [
"TV",
"cameras"
]
}
},
"fields": [
"_id",
"_rev"
],
"sort": [
{
"_id": "asc"
}
]
}
Or you can use
https://{url_database}/{name_database}/_design/{name_design}/_search/{name_search}?q=category:TV

MongoDB: Query model and check if document contains object or not, then mark / group result

I have a Model called Post, witch contains an property array with user-ids for users that have liked this post.
Now, i need to query the post model, and mark the returned results with likedBySelf true/false for use in by client - is this possible?
I dont have to store the likedBySelf property in the database, just modify the results to have that property.
A temporary solution i found was to do 2 queries, one that finds the posts that is liked by user x, and the ones that have not been liked by user x, and en map (setting likedBySelf true/false) and combine the 2 arrays and return the combined array. But this gives some limitations to other query functions such as limit and skip.
So now my queries looks like this:
var notLikedByQuery = Post.find({likedBy: {$ne: req.body.user._id}})
var likedByQuery = Post.find({likedBy: req.body.user._id})
(I'm using the Mongoose lib)
PS. A typical post can look like this (JSON):
{
"_id": {
"$oid": "55fc463c83b2d2501f563544"
},
"__t": "Post",
"groupId": {
"$oid": "55fc463c83b2d2501f563545"
},
"inactiveAfter": {
"$date": "2015-09-25T17:13:32.426Z"
},
"imageUrl": "https://hootappprodstorage.blob.core.windows.net/devphotos/55fc463b83b2d2501f563543.jpeg",
"createdBy": {
"$oid": "55c49e2d40b3b5b80cbe9a03"
},
"inactive": false,
"recentComments": [],
"likes": 8,
"likedBy": [
{
"$oid": "558b2ce70553f7e807f636c7"
},
{
"$oid": "559e8573ed7c830c0a677c36"
},
{
"$oid": "559e85bced7c830c0a677c43"
},
{
"$oid": "559e854bed7c830c0a677c32"
},
{
"$oid": "559e85abed7c830c0a677c40"
},
{
"$oid": "55911104be2f86e81d0fb573"
},
{
"$oid": "559e858fed7c830c0a677c3b"
},
{
"$oid": "559e8586ed7c830c0a677c3a"
}
],
"location": {
"type": "Point",
"coordinates": [
10.01941398718396,
60.96738099591897
]
},
"updatedAt": {
"$date": "2015-09-22T08:45:41.480Z"
},
"createdAt": {
"$date": "2015-09-18T17:13:32.426Z"
},
"__v": 8
}
#tskippe you can use a method like following to process whether the post is liked by the user himself and call the function anywhere you want.
var processIsLiked = function(postId, userId, doc, next){
var q = Post.find({post_id: postId});
q.lean().exec(function(err,res){
if(err) return utils.handleErr(err, res);
else {
if(_.find(doc.post.likedBy,userId)){ //if LikedBy array contains the user
doc.post.isLiked = true;
} else {
doc.post.isLiked = false;
}
});
next(doc);
}
});
}
Because you are using q.lean() you dont need to actually persist the data. You need to just process it , add isLiked field in the post and send back the response. **note that we are manuplating doc directly. Also you chan tweek it to accept doc containing array of posts and iterating it and attach an isLiked field to each post.
I found that MongoDB's aggregation with $project tequnique was my best bet. So i wrote up an aggregation like this.
Explanation:
Since i want to keep the entire document, but $project purpose is to modify the docs, thus you have to specify the properties you want to keep. A simple way of keeping all the properties is to use "$$ROOT".
So i define a $project, set all my original properties to doc: "$$ROOT", then create a new property "likedBySelf", which is marked true / false if a specified USERID is in the $likedBy set.
I think that this is more clean and simple, than querying every single model after a query to set a likedBySelf flag. It may not be faster, but its cleaner.
Model.aggregate([
{ $project: {
doc: "$$ROOT",
likedBySelf: {
$cond: {
"if": { "$setIsSubset": [
[USERID],
"$likedBy"
]},
"then": true,
"else": false
}
}
}}
]);

Resources