Finding documents that are valid on a specific date - mongodb

I have some data stored in a mongodb collection similar to:
{"_id": 1, "category": "food", "name": "chips", "price": 1.50, "effectiveDate": ISODate("2013-03-01T07:00:00Z")}
{"_id": 2, "category": "food", "name": "chips", "price": 1.75, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
{"_id": 3, "category": "food", "name": "chips", "price": 1.90, "effectiveDate": ISODate("2013-03-10T07:00:00Z")}
{"_id": 4, "category": "beverage", "name": "pop", "price": 2.00, "effectiveDate": ISODate("2013-03-01T07:00:00Z")}
{"_id": 5, "category": "beverage", "name": "pop", "price": 2.25, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
{"_id": 6, "category": "beverage", "name": "pop", "price": 1.80, "effectiveDate": ISODate("2013-03-10T07:00:00Z")}
In mongodb, how would I go about writing a query that would return the documents that were active on a specific date, grouped by the category?
If I specified March 6, 2013 I'd expect to see the results:
{"_id": 2, "category": "food", "name": "chips", "price": 1.75, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
{"_id": 5, "category": "beverage", "name": "pop", "price": 2.25, "effectiveDate": ISODate("2013-03-05T07:00:00Z")}
I am new to mongo and have been trying to do this using group, aggregate and mapreduce but have been just spinning in circles.

To give you a real good answer, I need more details of your code and what you are trying to do. But if I understand right, I think you can solve that using only the aggregation framework. You should know that the aggregation framework uses the pipeline concept, in other words, the result of each step is used as the entry to the following.
My query:
db.yourcollection.aggregate([
/* First exclude everything that is superior to a given date */
{$match:{effectiveDate:{$lte:new Date(2013,2,6)}}},
/* Sort the rest by date, descending */
{$sort:{effectiveDate:-1}},
/* Group by name+category and grab only the first result
(the newest below that date) */
{$group:{_id:{name:'$name',category:'$category'}, effectiveDate:{$first:"$effectiveDate"},price:{$first:"$price"}}},
/* You said you want the results grouped by category.
This last $group does that and returns all matching products inside an array
It also removes the duplicates */
{$group:{_id:'$_id.category',products:{$addToSet:{name:"$_id.name",price:"$price",effectiveDate:"$effectiveDate"}}}}
])
The output is like this:
{
"result": [
{
"_id": "food",
"products": [
{
"name" : "chips",
"price" : 1.75,
"effectiveDate" : ISODate("2013-03-05T07:00:00Z")
}
]
},
{
"_id" : "beverage",
"products": [
{
"name" : "pop",
"price" : 2.25,
"effectiveDate" : ISODate("2013-03-05T07:00:00Z")
}
]
}
],
"ok":1
}
You can change the final output modifing the last $group or using a $project

Related

How to fetch records from mongoDB on the basis of duplicate data in multiple fields

The requirement is to fetch the document with some field having the same values in the particular collection..
For Example:
we have two documents below:
1. {
"_id": "finance100",
"status": "ACTIVE",
"customerId": "100",
"contactId": "contact_100",
"itemId": "profile_100",
"audit": {
"dateCreated": "2022-02-16T16:34:52.718539Z",
"dateModified": "2022-03-18T09:36:42.774271Z",
"createdBy": "41d38c187155427fa37c855a4d1868d1",
"modifiedBy": "41d38c187155427fa37c855a4d1868d1"
},
"location": "US"
}
2. {
"_id": "finance101",
"status": "ACTIVE",
"customerId": "100",
"contactId": "contact_100",
"itemId": "profile_100",
"audit": {
"dateCreated": "2022-02-16T16:34:52.718539Z",
"dateModified": "2022-03-18T09:36:42.774271Z",
"createdBy": "41d38c187155427fa37c855a4d1868d1",
"modifiedBy": "41d38c187155427fa37c855a4d1868d1"
},
"location": "US"
}
3. {
"_id": "finance101",
"status": "ACTIVE",
"customerId": "100",
"contactId": "contact_100",
"itemId": "profile_100",
"audit": {
"dateCreated": "2022-02-16T16:34:52.718539Z",
"dateModified": "2022-03-18T09:36:42.774271Z",
"createdBy": "41d38c187155427fa37c855a4d1868d1",
"modifiedBy": "41d38c187155427fa37c855a4d1868d1"
},
"location": "UK"
}
The following parameter should have the same values:
customerId
contactId
itemId
location
so, need the fetch those records, matching with these above parameters having the same values in all the documents.
So, it should fetch the first two records (1 and 2) because the values for customerId, contactId, itemId, and location is same in first two document and in 3rd document only the location value is different(so it will not be fetched).
Could you please share the appropriate mongo query to work for this. I tried aggeration but it did not work. Thanks in advance.
Need the fetch those records, matching with these above parameters having the same values in all the documents.

How to update specific field in mongoDB given conditions?

Given the following mongdoDB structure, how can i update the field isAvailable to false given that the shopName is "jamrt" and slug is "67626dae-1537-40d8-837d-483e5759ada0". This is my query but it does not work: Shop.find({ shopName: shopName}).update({products: {$elemMatch: {slug: slug}}}, { $set: { isAvailable: req.body.isAvailable} } Thanks!
"shopName": "jmart",
"products": [{
"id": 1,
"name": "Clean and Clear Deep Clean Cleanser 100g",
"slug": "8d1c895c-6911-4fc8-a34c-89c6948233d7",
"price": 4.5,
"discount_price": 0,
"category": "Health and Beauty",
"sale": false,
"subcategory": "personal care",
"color": "black",
"article": "Clean and Clear",
"quantity": 9,
"img": "https://firebasestorage.googleapis.com/v0/b/swifty-products.appspot.com/o/Jmart%2FBeauty%2FClean%20and%20Clear%20Deep%20Clean%20Cleanser%20100g.jpg?alt=media",
"vendor": {
"id": 1,
"name": "Clean and Clear"
},
"ratings": {
"star_ratings": 0,
"votes": 0
},
"isAvailable": true
}, {
"id": 2,
"name": "Colgate Total Pro Breath Health",
"slug": "67626dae-1537-40d8-837d-483e5759ada0",
"price": 4.5,
"discount_price": 0,
"category": "Health and Beauty",
"sale": false,
"subcategory": "personal care",
"color": "black",
"article": "Colgate",
"quantity": 9,
"img": "https://firebasestorage.googleapis.com/v0/b/swifty-products.appspot.com/o/Jmart%2FBeauty%2FColgate%20Total%20Pro%20Breath%20Health.jpg?alt=media",
"vendor": {
"id": 2,
"name": "Colgate"
},
"ratings": {
"star_ratings": 0,
"votes": 0
},
"isAvailable": true
},
]
In your case, you are trying to update only the matching sub documents.
The $elemMatch operator while using in projection updates only the first matching sub document.
The $elemMatch operator while using in find updates all the fields of the matching document.
This solution might help you.
With your case, the solution might be the below in mongodb query:
db.Shop.update({"shopName":"jmart","products.slug":"67626dae-1537-40d8-837d-483e5759ada0"}, {$set: {“products.$[i].isAvailable”: false}}, {arrayFilters: [{“i.slug”: "67626dae-1537-40d8-837d-483e5759ada0"}]})

DeleteMany in mongoDb only deleting first condition data

I am trying to delete multiple rows in mongo db by multiple conditions but i see only first condition data is deleted. Please Someone help. Sharing the data for clarification.
Data:
{
"_id": ObjectId("601fcc2f4bae4bc542085e8b"),
"name": "tutoria 2",
"id": 19,
"rollno": 1020,
"amount": "one hundred only"
}{
"_id": ObjectId("601fce9a4bae4bc542085e8e"),
"name": "tutoria 2",
"id": 19,
"rollno": 1020,
"amount": "hundred only"
}{
"_id": ObjectId("601fcea44bae4bc542085e8f"),
"name": "tutoria 3",
"id": 10,
"rollno": 1020,
"amount": "hundred only"
}{
"_id": ObjectId("601fceb04bae4bc542085e90"),
"name": "tutoria 4",
"id": 21,
"rollno": 1020,
"amount": "hundred only"
}
Query:
db.movie.deleteMany(
{
"name": "tutoria 4"
},
{
"id": 10
}
)
{
"acknowledged": true,
"deletedCount": 1
}
Not sure what's wrong with mongoose.
It worked by this.
db.movie.deleteMany({$or:[{"name":"tutoria 098"},{"id":20}]})
Try this one:
db.collection.deleteMany({
$and: [
{name:"tutoria 4"},
{id:10}
]
})
All matching query $and should return true to delete filtered documents, otherwise it won't delete at all.

Sum value of elements and update field Mongo db

How Can I sum price on array "Elemets" and set on Document field Value?
I know how to do It in sql but I,m beginner in mongo.
{
"Document": [
{
"Id": 1,
"Type": "FV",
"Number": 34521,
"Year": 2020,
"Date": "2020-01-01T00:00:00",
"Value": 27.68,
"Elements": [
{
"Id": 1,
"DocumentId": 1,
"ProductId": 1,
"Quantity": 5.00,
"Price": 17.50,
"Task": 0.23
},
{
"Id": 2,
"DocumentId": 1,
"ProductId": 2,
"Quantity": 3.00,
"Price": 24.50,
"Task": 0.23
},
]
},
If you are using MongoDB 4.2, you can use $reduce to calculate the sum in the pipeline form of update.

Finding values in mongodb collection with multiple occurence

I have a collection with documents as follows :
{id: 1, "year": "12", "type": "checking", "location": "nyc", "category" : "Admin"}
{id: 2, "year": "15", "type": "checking", "location": "ma", "category": "Normal"}
{id: 3, "year": "12", "type": "credit", "location": "nyc","category": "Admin"}
{id: 4, "year": 12, "type": "checking", "location": "nyc", "category" : "Admin"}
Now, i want to count the 'type' that appears more than once meeting the criteria location:nyc, year:12, category: Admin
So the desired result is
{"Checking": 2}
What way can I achieve the above?
Use $match and $group
db.collectionname.aggregate({$match: {location: 'nyc', year:'12', category:'Admin'}},
{$group: {_id:'$type', count:{$sum:1}}},
{$match:{count: {$gt:1}}}
)