Searching many parameters in a Cloudant noSQL DB - node.js

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

Related

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

How to use mongodb aggregation to transform document in nested array

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'}
}}
])

MongoDB: How to query field array by _id

how to get the specific message by _id.
I have a database schema as shown below:
{
"_id": ObjectID("5846eedaf0b51ed02ed846e2"),
"name": "conversation name"
"messages": [
{
"_id": ObjectID("584820a96866b6283361a4b9"),
"content": "hello",
"status": 0,
"type": 0
},
{
"_id": ObjectID("584820d56866b6283361a4ba"),
"content": "voices/1481122005696.mp3",
"status": 0,
"type": 3
}
]
}
db.collection.find({}, {
'messages': {
$elemMatch: {
'_id': ObjectId("584820a96866b6283361a4b9")
}
}
})
This will match on all documents in your collection, and return the matching array subfield in the projection. See the link posted in Yogesh's comment.
Since you tagged mongoose, I put it like this, but the query syntax is valid because is part of mongoDB query syntax:
Conversation.findOne({
"messages._id": "584820a96866b6283361a4b9"
}, function(err, document){
if (err) {
console.log(err)
return
}
var message = document.messages.id("584820a96866b6283361a4b9")
})
The finOne() method will return the full parent document with all the messages (subdocuments) and other properties. You then need to filter out the subdocument. Mongoose provides a way to do this easily with: document.messages.id("584820a96866b6283361a4b9") where document would be the data object passed to the find callback
See:
Match a Field Without Specifying Array Index on
https://docs.mongodb.com/v3.2/tutorial/query-documents/#query-on-arrays

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