mongodb-query

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

Query MongoDb aggregate join two collections

时光怂恿深爱的人放手 提交于 2020-03-21 06:18:09
问题 I need help for querying mongoDb So I have two collections like Collection A: {someField: "123", anotherField: "456"}, {someField: "1234", anotherField: "4567"} Collection B {someField: "123", otherField: "789"} with Query: db.A.aggregate([ { $lookup: { from: "B", let: { someField: "$someField", otherField: "$otherField" }, pipeline: [ { $match: { $expr: { $and: [ { $eq: [ "$someField", "$$someField" ] }, { $eq: [ "$otherField", "789" ] } ] } } }, ], as: "B" } } ]) I get all collection A,

Docker and mongo-go-driver “server selection error”

痴心易碎 提交于 2020-03-21 05:52:26
问题 I had creating a MongoDB replica set using Docker and I can access using console or Robo3T and run any query. The containers appear like this: $ docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES efe6ae03323d mongo "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:30001->27017/tcp mongo1 57d2701c8a43 mongo "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0:30002->27017/tcp mongo2 7553966b9ff5 mongo "docker-entrypoint.s…" 10 minutes ago Up 10 minutes 0.0.0.0

How to calculate the percentile?

陌路散爱 提交于 2020-03-18 04:11:25
问题 I have access logs such as below stored in a mongodb instance: Time Service Latency [27/08/2013:11:19:22 +0000] "POST Service A HTTP/1.1" 403 [27/08/2013:11:19:24 +0000] "POST Service B HTTP/1.1" 1022 [27/08/2013:11:22:10 +0000] "POST Service A HTTP/1.1" 455 Is there an analytics function like PERCENTILE_DISC in Oracle to calculate the percentile? I would like to calculate latency percentiles over a period of time. 回答1: There still appears to be no native way to calculate percentiles but by

How to calculate the percentile?

≡放荡痞女 提交于 2020-03-18 04:09:25
问题 I have access logs such as below stored in a mongodb instance: Time Service Latency [27/08/2013:11:19:22 +0000] "POST Service A HTTP/1.1" 403 [27/08/2013:11:19:24 +0000] "POST Service B HTTP/1.1" 1022 [27/08/2013:11:22:10 +0000] "POST Service A HTTP/1.1" 455 Is there an analytics function like PERCENTILE_DISC in Oracle to calculate the percentile? I would like to calculate latency percentiles over a period of time. 回答1: There still appears to be no native way to calculate percentiles but by