How to use regex in addFields aggregate with mongodb - node.js

I want to use regex in addFiels in aggregate with mongodb, but I am not able to do that below is my code which I have tried
$addFields: {slug: {$title: {$regex: '/^[^_s]*$/'}}}
but this is giving mongo error. Please find me solution. My expected output should be
slug: "This-is-my-1st-adventure". In my db title: "This is my 1st adventure"

The following query can do the trick. We are using $reduce to replace space with a hyphen.
db.collection.aggregate([
{
$addFields:{
"slug":{
$let:{
"vars":{
"array":{
$split:["$title"," "]
}
},
"in":{
$reduce:{
"input":"$$array",
"initialValue":"",
"in":{
$concat:["$$value","-","$$this"]
}
}
}
}
}
}
},
{
$addFields:{
"slug":{
$substr:["$slug",1,{ $strLenBytes: "$slug" }]
}
}
}
]).pretty()
Data set:
{
"_id" : ObjectId("5d84af0aebcbd560107c54af"),
"title" : "This is my 1st adventure"
}
{
"_id" : ObjectId("5d84af0aebcbd560107c54b0"),
"title" : "This is my 2nd adventure"
}
Output:
{
"_id" : ObjectId("5d84af0aebcbd560107c54af"),
"title" : "This is my 1st adventure",
"slug" : "This-is-my-1st-adventure"
}
{
"_id" : ObjectId("5d84af0aebcbd560107c54b0"),
"title" : "This is my 2nd adventure",
"slug" : "This-is-my-2nd-adventure"
}

Related

query to get all _id in array in mongdb?

I have following collection
{
"_id" : ObjectId("5acdb95d5ea63a27c1facf92"),
"venue" : ObjectId("5acdb95d5ea63a27c1facf8c"),
"author" : ObjectId("5ac8ba3582c2345af70d4658"),
}
{
"_id" : ObjectId("5acdb95d5ea63a27c1facf93"),
"venue" : ObjectId("5acdb95d5ea63a27c1facf8c"),
"author" : ObjectId("5ac8ba3582c2345af70d4658"),
}
{
"_id" : ObjectId("5acdb95d5ea63a27c1facf91"),
"venue" : ObjectId("5acdb95d5ea63a27c1facf8c"),
"author" : ObjectId("5ac8ba3582c2345af70d4658"),
}
how to get all _id in an array having same venue
My output should be like this
{array: ['5acdb95d5ea63a27c1facf91', '5acdb95d5ea63a27c1facf91', '5acdb95d5ea63a27c1facf93']}
Try this query:
db.colelction.aggregate([
{$match:{"venue" : ObjectId("5acdb95d5ea63a27c1facf8c")}},
{$group:{_id:null, array:{$push:"$_id"}}},
{$project:{_id:0}}
])
And the output is:
/* 1 */
{
"array" : [
ObjectId("5acdb95d5ea63a27c1facf92"),
ObjectId("5acdb95d5ea63a27c1facf93"),
ObjectId("5acdb95d5ea63a27c1facf91")
]
}
db.collection.aggregate(
// Pipeline
[
// Stage 1
{
$group: {
_id:{venue:'$venue'},
_ids:{$addToSet:'$_id'}
}
},
// Stage 2
{
$project: {
_ids:1,
_id:0
}
}
]
);

i am not able to query the sub document in mongodb

i am not able to query results in this query,
i want result based on detail.type (like fetch record where detail.type="one") and fetch only first 10 records in detail.numbers array
{
"_id" : ObjectId("5a27b609e101b6092b4ebf91"),
"city" : "Mumbai",
"detail" : [
{
"type" : "One",
"name" : "Some name",
"_id" : ObjectId("5a27b609e101b6092b4ebf92"),
"numbers" : [
"72598xxx78",
"81301xxx88",
"83302xxx30",
"84309xxx43",
"85309xxx77",
"86309xxx61",
"87270xxx88",
"85272xxx36",
"88272xxx23",
"85276xxx01"
]
},
{
"name" : "Some name",
"type" : "two",
"_id" : ObjectId("5a28e954d4f5a30527d92a32"),
"contact" : [
"72598xxx78",
"81301xxx88",
"83302xxx30",
"84309xxx43",
"85309xxx77",
"86309xxx61",
"87270xxx88",
"85272xxx36",
"88272xxx23",
"85276xxx01"
]
},
]
}
MongoDB facilitates querying over array elements using $elemMatch operator.
According to description as mentioned into above question as a solution to it please try executing following MongoDB query to fetch required data from MongoDB collection.
db.collection.find({
detail: {
$elemMatch: {
type: 'One'
}
}
}, {
_id: 1,
city: 1,
'detail.$': 1
})
db.collection.aggregate([
{
$project:{
detail:{
$map:{
input:{$filter:{input:"$detail",as:"d",cond:{$eq:["$$d.type","One"]}}},
as:"d",
in:{
"type" : "$$d.type",
"name" : "$$d.name",
"numbers":{$slice:["$$d.numbers",10]}
}
}
}
}
}
])

MongoDB - query field to include separate words [duplicate]

I want to use $or and $regex operators same time.
db.users.insert([{name: "Alice"}, {name: "Bob"}, {name: "Carol"}, {name: "Dan"}, {name: "Dave"}])
Using $regex works fine:
> db.users.find({name: {$regex: "^Da"}})
{ "_id" : ObjectId("53e33682b09f1ca437078b1d"), "name" : "Dan" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1e"), "name" : "Dave" }
When introducing $or, the response is changed. I expected the same response:
> db.users.find({name: {$regex: {$or: ["^Da"]}}})
{ "_id" : ObjectId("53e33682b09f1ca437078b1a"), "name" : "Alice" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1b"), "name" : "Bob" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1c"), "name" : "Carol" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1d"), "name" : "Dan" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1e"), "name" : "Dave" }
I also tried to change the order of the operators:
> db.users.find({name: {$or: [{$regex: "^Da"}, {$regex: "^Ali"}]}})
error: { "$err" : "invalid operator: $or", "code" : 10068 }
However, it seems that following query works fine, but it's a little bit long (name is repeated):
> db.users.find({$or: [{name: {$regex: "^Da"}}, {name: {$regex: "^Ali"}}]})
{ "_id" : ObjectId("53e33682b09f1ca437078b1a"), "name" : "Alice" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1d"), "name" : "Dan" }
{ "_id" : ObjectId("53e33682b09f1ca437078b1e"), "name" : "Dave" }
Is there any shorter way to use $regex and $or in queries like this?
The goal is to use $regex operator and not /.../ (real regular expressions).
The $or operator expects whole conditions so the correct form would be:
db.users.find({ "$or": [
{ "name": { "$regex": "^Da"} },
{ "name": { "$regex": "^Ali" }}
]})
Or of course using $in:
db.users.find({ "name": { "$in": [/^Da/,/^Ali/] } })
But it's a regex so you can do:
db.users.find({ "name": { "$regex": "^Da|^Ali" } })
It is been a while. However, I would add case insensitive to the regex query like the query below. So that, it doesn't matter if names were saved into the database with capital letters:
db.users.find({ "name": { "$regex": "^Da|^Ali", "$options": "i" } })
Hope it helps
It seems when you have $and or $or and multiple search based and used at least one $regex you have to use $regex for all conditions.
First from below works ok, second more like $or operator.
db.big_data.users.find(
{ $and: [
{ sex: { $regex: /^M.*/ } },
{ name: { $regex: /^J.*/ } }
] })
db.big_data.users.find({ $and: [ {sex: "M"}, { name: { $regex: /^J*/m } } ] })
you can use OR operator like
db.collName.find({ "name": { "$regex": "^Da|^Ali" ,"$options": "i" } })
and operator
db.collName.find({ "name": { "$regex": "Ali" ,"$options": "i" } })
for more info
source - https://www.cs.jhu.edu/~jason/405/lectures1-2/sld049.htm

Mongo Node driver how to get all fields of $max aggregate from an array of objects

I have a collection called "products" which has an array of "bids" objects.
I want to find out the Maximum bid for each product, for this I am aggregating Products on $max with $bids.bidamount field. However this is only giving me the largest bid amount. How do I project all the bid fields for the max aggregation.
Here is a sample document
{
"_id" : ObjectId("58109a5138fe12215cfdc064"),
"product_id" : 2,
"item_name" : "Auction Item1",
"item_description" : "Test",
"seller_name" : "ak#gmail.com",
"item_price" : "20",
"item_quantity" : 7,
"sale_type" : "Auction",
"posted_at" : "2016:10:26 04:58:09",
"expires_at" : "2016:10:30 04:58:09",
"bids" : [
{
"bid_id" : 1,
"bidder" : "ak#gmail.com",
"bid_amount" : 300,
"bit_time" : "2016:10:26 22:36:29"
},
{
"bid_id" : 2,
"bidder" : "ak#gmail.com",
"bid_amount" : 100,
"bit_time" : "2016:10:26 22:37:29"
}
],
"orders" : [
{
"buyer" : "ak#gmail.com",
"quantity" : "2"
},
{
"buyer" : "ak#gmail.com",
"quantity" : "3"
}
]
}
Here is my mongo query:
db.products.aggregate([
{
$project: {
bidMax: { $max: "$bids.bid_amount"}
}
}
])
which gives the following result:
{
"_id" : ObjectId("58109a5138fe12215cfdc064"),
"bidMax" : 300
}
db.products.aggregate([{$unwind:"$bids"},{$group:{_id:"$_id", sum:{$sum:"$bids.bid_amount"}}},{$project:{doc:"$$ROOT", _id:1, sum:1}, {$sort:{"sum":-1}},{$limit:1}]),
which return something like { "_id" : ObjectId("5811b667c50fb1ec88227860"), "sum" : 600, doc:{your document....} }
This should do it:
db.products.aggregate([{
$unwind: '$bids'
}, {
$group: {
_id: '$products_id',
maxBid: {
$max: '$bids.bid_amount'
}
}
}])
db.collectionName.aggregate(
[
{
$group:
{
_id: "$product_id",
maxBidAmount: { $max: "$bids.bid_amount" }
}
}
]
)
Hey use this query, you will get the result.

Update and/or add array element properties using req.body via Mongoose?

I have the following document:
{
"_id" : ObjectId("503b83dfad79cc8d26000004"),
"pdfs" : [
{
"title" : "Test document",
"pdf_id" : ObjectId("504f6793ce351a595d000004"),
"created_at" : ISODate("2012-09-11T16:32:19.276Z")
},
{
"title" : "Some other doc",
"pdf_id" : ObjectId("502bf124b4642341230003f0"),
"created_at" : ISODate("2012-09-11T11:34:19.276Z")
}
]
}
Now in an incoming form via req.body, I have 2 fields: title and description.
I want to update title and insert description for a specified pdf_id, how do I do that?
So in the end, my document will now look like:
{
"_id" : ObjectId("503b83dfad79cc8d26000004"),
"pdfs" : [
{
"title" : "This is an UPDATED title",
"description" : "It has an ALL NEW description",
"pdf_id" : ObjectId("504f6793ce351a595d000004"),
"created_at" : ISODate("2012-09-11T16:32:19.276Z")
},
{
"title" : "Some other doc",
"pdf_id" : ObjectId("502bf124b4642341230003f0"),
"created_at" : ISODate("2012-09-11T11:34:19.276Z")
}
]
}
Just to be clear, I'm really just looking for the Mongoose update syntax.
You can use the $ positional operator to refer to the matched pdfs array element in your $set:
Model.update(
{ 'pdfs.pdf_id': pdf_id },
{ $set: {
'pdfs.$.title': title,
'pdfs.$.description': description
}}, function (err, numAffected) { ... }
);

Resources