aggregation-framework

How to match a string consisting special character like hyphen and spaces

懵懂的女人 提交于 2020-04-07 08:11:48
问题 I have a string as: word : 'A-Scan Ultrasonic' How to match this with : 'A Scan Ultrasonic' I have tried my luck as {word:{$regex:".*A Scan Ultrasonic.*",$options: 'i'}} But this doesn't fetch any result May I know how this can be matched , any help is appreciated , TIA 回答1: It's actually no mongo thing, you should use proper regex. Try with this: { name: { $regex: /.*A(-|\s)Scan(-|\s)Ultrasonic.*/}} 来源: https://stackoverflow.com/questions/60810426/how-to-match-a-string-consisting-special

Add a value to to the record using aggregation

我与影子孤独终老i 提交于 2020-03-28 06:39:17
问题 sample of my document { _id: "bmasndvhjbcw", name: "lucas", occupation: "scientist", present_working:true, age: 55, location: "texas", }, { _id: "bmasndvhjbcx", name: "mark", occupation: "scientist", age: 45, present_working:true, location: "texas", }, { _id: "bmasndvhjbca", name: "stuart", occupation: "lab assistant", age: 25, location: "texas", }, { _id: "bmasndvhjbcq", name: "cooper", occupation: "physicist", age: 69, location: "texas" } ] For the records which doesn't have present_working

how to work with $lookup and $regex search in mongodb

萝らか妹 提交于 2020-03-26 04:03:24
问题 var query = req.params.query; const messages = await Message.aggregate([ { $lookup: { from: 'users', localField: 'reference_id', foreignField: '_id', as: 'users' } }, {'text': {'$regex': `.*${query}.*`}} ]) $lookup is providing me right information but $regex is not properly working When add {'text': {'$regex': .*${query}.* }} tells me Arguments must be aggregate pipeline operators after $lookup i have data { "response": { "messages": [ { "_id": "5e4a8e640b92852e110af62f", "text": "Gsshha",

Is there a way to use the aggregate pipeline method for this query

不打扰是莪最后的温柔 提交于 2020-03-25 18:22:18
问题 I have a database of over 2000 reviews for products on an online store, and I am trying to find all of the users who have written minimum 5 reviews, and I would like to list their names, number of reviews, and reviewer ID I understand that I have to use aggregate method to do this but when trying I have no luck as I am still new to mongodb I have added a picture to show an example of some of the data in the database An example of one entry into the database is as below; { "_id": ObjectId(

MongoDB find & group similar items

浪子不回头ぞ 提交于 2020-03-25 05:51:49
问题 I have a table for teams that looks as shown below: I am just displaying the fields that I am interested in. I was wondering how I can get the teams with goals for and against that have the same value of goals for and goals against. For example in the table above it should be: Italy 4 5 Mexico 4 5 England 3 5 Chile 3 5 The answers should be distinct as well. This is what I have so far: var team1 = db.Teams.find(); var team2 =db.Teams.find(); team1.forEach(function(item){team2.forEach(function

MongoDB aggregation to add missing months between two dates after grouping on date field

大城市里の小女人 提交于 2020-03-23 08:06:22
问题 I have a query that is returning the total number of entries in a collection per year-month, grouped by a location. This is returning data exactly as I need it if the location has results for the year-month in question. However, is it possible to insert an entry for a month that does not have a result? For instance lets say if my $match has a date range of 01-2019 to 12-2019. I would like to have all 12 entries for the month with a default of total: 0. Is this possible Truncated Schema : {

MongoDB Aggregation : Double lookup, and merge lookup response to respective object

自闭症网瘾萝莉.ら 提交于 2020-03-23 08:01:07
问题 I'm trying an aggregation but I can't find the right pipeline to do it. So, this is a part of my document model : //company.js { "_id" : "5dg8aa8c435b1e2868c841f6", "name" : "My Corp", "externalId" : "d7f348c9-c69b-69c4-923c-91458c53dc22", "professionals_customers" : [ { "company" : "6f4d01eb3b948150c2aad9c0" }, { "company" : "5dg7aa8c366b1e2868c841f6", "contact" : "5df8ab5c355b1e2999c841f7" } ], } I try to return the professionnal customers fields hydrated with data, like a classic populate

Match multiple conditions in an aggregate under expressions

梦想与她 提交于 2020-03-22 12:56:31
问题 sample format of document: { "_id": { "$oid": "5e158e2de6facf7181cc368f" }, "word": "as luck would have it", } I am trying to match multiple conditions in an expression as: query = { "$match": { "$expr": {"$eq": [{"$strLenCP": "$word"}, 6], '$lt': [ { '$size': { '$split': [ "$word", " " ] } }, 2 ] } } } And pipe line as follows: pipeline = [query] cursor_objects = db['test'].aggregate(pipeline) In the above query I am trying to achieve word length must be 6 and it doesn't contain any spaces

Count no.of instances of string in a field across documents grouped on another field in MongoDB?

和自甴很熟 提交于 2020-03-15 06:40:26
问题 I've got a specific use case and I'm trying to find a way to do it in one aggregation pipeline and preferably without the need to hardcode any data values. I want to group documents based on one property and see a count of values for a particular field within the document. Example data: { "flightNum": "DL1002", "status": "On time", "date": 20191001 }, { "flightNum": "DL1002", "status": "Delayed", "date": 20191002 }, { "flightNum": "DL1002", "status": "On time", "date": 20191003 }, {

$sum aggregation in mongodb nodej driver

笑着哭i 提交于 2020-03-05 06:04:05
问题 collection 1: admin { "_id" : ObjectId("5e27fd3da42d441fe8a89580"), "mappedcustomers": [ ObjectId("5e2555783405363bc4bf86c5"), ObjectId("5e2555783405363bc4bf86c0"), ObjectId("5e2555783405363bc4bf86c4") ], "phoneNo" : 9897654358, "name" : "acdbcs" } collection 2: productOrders Tt has lots of documents, our concern is only for: "productOrderedForDate" : ISODate("2020-02-04T18:30:00Z") [tomorrow order]. "productOrderedForDate" : ISODate("2020-02-28T18:30:00Z") [last week order] [ { "_id":