Is it Possible to Create multi Single-field indexes in mongodb? - node.js

Is it valid to create more then 1 Single-field index in mongodb?:
Let's assume I have a collection of products, and in some queries I want to find by Category whereas in others I want to find by date
Will it be valid to create 2 single-field indexes for that collection?
mongoose:
schema.index({ Category:1});
schema.index({ date: 1 });

Related

Mongodb upadate the whole collection according to the data

I needs to update the Mongodb whole collection according to the data on my website.
Schema({ title: "data" }).which_function_should_I_call
when I call the first time it has 20 data item update the collection 20 documents - collection documents length should be 20.
after the second call data has 5 data item the collection length should be 5 documents.

Mongoose: Effective method to iterate over records to update date values from a given array?

Let's say I have the ids of products as array and I want to add 6 months to each date of the products which is in array
my solution:
for(let product of product_id_array){
db.Product.update({"_id" : product.id},{$set: { "endDate" : "add 6 months"}})
}
the array is in this format:
product_id_array = [{id: "5fdbc7fafff547f06b4d8bfd"}, {id: "e5dbc7fafff547f06b4d8bfd"}]
Do we have any other way for this? like putting array inside the update query to execute faster instead of loop?

Is there a way to query for documents that have a certain millisecond in a timestamp? [Mongo]

I have a collection on my db with a "created" field that is a timestamp.
What I need to do is somehow get the milliseconds on that timestamp, divide it by 10 and then floor it and THEN match it to a number.
What I'm trying to achieve with this is have a random spread of this documents into "batches" that range from 0-100.
Is there a way to do all this in one query? Basically something like:
Tried aggregation to add that field but then I'm getting all the documents and filtering, was wondering if there's a way to do all this "in-query"
collection.find({<created.convertToMilliseconds.divideByTen>: X }})
// X being any number from 0 to 100
I

How to filter docs in MongoDB and then get one document ignoring duplicates?

So this is the schema of my document
"number" : 4657821598,
"hospitalID" : "mahaveer_bgm",
"value" : 1
first i would like to filter based on the hospitalID and then i want to remove all the duplicate for number.
for example if there are 2 docs with same number after hospitalID filter get me only the latest document.
Thanks in Advance
You can use below aggregation.
The below query applies the filter using $match followed by $sort on number desc and $group on number to pick the latest document.
$$ROOT to reference the whole document.
db.col.aggregate([
{"$match":{"hospitalID":"mahaveer_bgm"}},
{"$sort":{"number":-1},
{"$group":{
"_id":"$number",
"name":{"$first":"$$ROOT"}
}}
])

How to query a view in cloudant using startkey and endkey where key has two elements in it?

I have a requirement to filter some records on the basis of period for example get me all products count between 201704 and 201705.
So my emit is something like:
emit([doc.productId, doc.period],1)
and reduce used is _count. But when i am trying to filter on startkey and endkey it gives me all records. I am trying something like:
startkey=["201604"]&endkey=[{},"201605"]
where my key structure is something like key:
[
"aws-60826348802",
201703
]
I dont want to reverse the order of elements in key as i want a count on ids by defining the group_level=1. Could anyone help me out. I did not get any solution yet.
In order to be able to select (with startkey/endkey), your index needs to be in in that order. Let's say your docs look like this:
{
"_id": "someid",
"product": "aws-60826348802",
"date": "201604",
"price": 42.42
}
You could create a map function to index the documents in date order like so:
function(doc) {
emit([doc.date, doc.product], doc.price);
}
You now have a two-dimensional key (on date & product code) and the value is emitted is the price.
This allows you to select items by date:
?startkey=["201604"] // find items where the date is >= 201604
?startkey=["201604"]&endkey=["201605"] // find items in April 2016
if you choose a reducer (_count, _sum, or _stats) you can also perform aggregation:
?startkey=["201601"]&endkey=["201701"]&group_level=1 // find items in 2016, group by month
?startkey=["201601"]&endkey=["201701"]&group_level=2 // find items in 2016, grouped by month and product code
This could get you monthly totals of product sales, for example.

Resources