天道酬勤,学无止境

mongodb-query

How to retrieve distinct keys inside an object in MongoDB

I have this in MongoDB: { "_id" : ObjectId("58fb35531eb5df245d5d434f"), "name" : "d1.html", "indexation" : { "Citroen" : 1, "color" : 1, "Marca" : 1, "rojo" : 1 } } { "_id" : ObjectId("58fb35531eb5df245d5d4350"), "name" : "d2.html", "indexation" : { "blancos" : 1, "ocasión" : 1, "Madrid" : 1, "Coches" : 1, "rojo" : 1, "coches" : 1 } } { "_id" : ObjectId("58fb35531eb5df245d5d4351"), "name" : "d3.html", "indexation" : { "rojos" : 1, "Ocasión" : 1, "marcas" : 1, "Madrid" : 1, "blancas" : 1, "autos" : 1, "de" : 1 } } You can see an image containing the above: And I would like to get the distinct

2022-01-18 18:39:01    分类:问答    arrays   mongodb   mongodb-query

Using aggregate $lookup and $mergeObjects

I want to join collection. before, I used only lookup, so that I could get separated field that is joined. but I need to get result similar mysql join. I noticed there is $lookup and $mergeObjects for this action but not working well. user collection model. { "_id": ObjectId("xxxxxxx"), //this is default id from mongoDB "name": 'admin user', "email": 'admin@test.com', "password": 'xxxxxxxx', "roles": [ { "id": 0, "approved": true },{ "id": 2, "approved": true } ] },{ "_id": ObjectId("xxxxxxx"), //this is default id from mongoDB "name": 'userOne', "email": 'user@test.com', "password": 'xxxxxxxx

2022-01-18 18:29:12    分类:问答    javascript   node.js   mongodb   mongodb-query   aggregation-framework

How do I sort a collection based on values in an array

I have collection named result with following values:- > db.result.findOne() { "_id" : ObjectId("53b05264421aa97e980ba404"), "result" : [ { "attempted" : 49, "subject_total_marks" : 50, "score" : 15, "correct_subject_answer" : 15, "subject" : "Biology" }, { "attempted" : 30, "subject_total_marks" : 30, "score" : 4, "correct_subject_answer" : 4, "subject" : "Chemistry" }, { "attempted" : 20, "subject_total_marks" : 20, "score" : 7, "correct_subject_answer" : 7, "subject" : "Physics" }, { "attempted" : 99, "correct_subject_answer" : 26, "score" : 26, "subject_total_marks" : 100, "subject" :

2022-01-18 17:03:05    分类:问答    python   mongodb   sorting   mongodb-query   aggregation-framework

MongoDB aggregation $elemMatch inside $lookup stage

I have a MongoDB collection that is looking like this: { players: [ {uuid: "A"}, {uuid: "B"} ] }, { players: [ {uuid: "A"}, {uuid: "C"} ] }, { players: [ {uuid: "D"}, {uuid: "E"} ] } I want to use results of a previous aggregation stage and now find all documents where a player shows up, using a $lookup stage: from: "collection", pipeline: [ { $match: { players: { $elemMatch: { uuid: "$playerId" } } //using "players.uuid": "$playerId" doesn't work either } } ], as: "field" The input to my $lookup stage looks like this: { "playerId" : "A" } { "playerId" : "B" } { "playerId" : "C" } This query

2022-01-18 11:47:51    分类:问答    mongodb   mongodb-query   aggregation-framework

Save a subset of MongoDB(3.0) collection to another collection in Python

I found this answer - Answer link db.full_set.aggregate([ { $match: { date: "20120105" } }, { $out: "subset" } ]); I want do same thing but with first 15000 documents in collection, I couldn't find how to apply limit to such query (I tried using $limit : 15000, but it doesn't recognize $limit) also when I tried - db.subset.insert(db.full_set.find({}).limit(15000).toArray()) there is no function toArray() for output type cursor. Guide me how can I accomplish it?

2022-01-18 05:10:59    分类:问答    python   mongodb   mongodb-query   aggregation-framework   mongodb-aggregation

Update embedded document in Mongodb: Performance issue?

I am new to Mongodb and I heard that Mongodb is good for massive amount of read and write operations. Embedded document is one of the features that make it happen. But I am not sure if it is also a cause of performance issue. Book document example: { "_id": 1, "Authors": [ { "Email": "email", "Name": "name" } ], "Title": "title", ... } If there are thousands of books by one author, and his email needs to be updated, I need to write some query which can search through all book documents, pick out those thousands ones with this author update author's email field across these book documents These

2022-01-18 04:31:29    分类:问答    mongodb   mongodb-query

How to return all fields without specify after $group stage

How to sort the child array in a document and keep the parent fields? I have a collection like this: { "_id" : ObjectId("5d1757929831633ff8abb638"), "name" : "Carraro Exlusive", "description" : "Carraro Exlusive description", "image" : "image-15", "__v" : 0, "sellers" : [ { "_id" : ObjectId("5d1757929831633ff8abb639"), "product" : ObjectId("5d1757929831633ff8abb638"), "seller" : ObjectId("5d1518582f9254189ca92e59"), "price" : 13, "stock" : 5, "__v" : 0 }, { "_id" : ObjectId("5d175b60cf85a22f745235ee"), "product" : ObjectId("5d1757929831633ff8abb638"), "seller" : ObjectId(

2022-01-17 17:12:20    分类:问答    mongodb   mongodb-query   aggregation-framework

Get documents with nested objects matching count condition

I am a mongo noob and am working with a mongo collection with records that look like so: { "cats" [ { "name": "fluffy", "color": "red", }, { "name": "snowball", "color": "white", }, ] { I would like to perform a query that gets all records that have more than 1 white cats. MapReduce looks promising, but seems like overkill. Any help is appreciated.

2022-01-17 07:50:13    分类:问答    mongodb   mongodb-query   aggregation-framework

$group inner array values without $unwind

I want to group objects in the array by same value for specified field and produce a count. I have the following mongodb document (non-relevant fields are not present). { arrayField: [ { fieldA: value1, ...otherFields }, { fieldA: value2, ...otherFields }, { fieldA: value2, ...otherFields } ], ...otherFields } The following is what I want. { arrayField: [ { fieldA: value1, ...otherFields }, { fieldA: value2, ...otherFields }, { fieldA: value2, ...otherFields } ], newArrayField: [ { fieldA: value1, count: 1 }, { fieldA: value2, count: 2 }, ], ...otherFields } Here I grouped embedded documents

2022-01-17 07:07:58    分类:问答    mongodb   mongodb-query   aggregation-framework

How do I solve: 'MongoError: $where is not allowed in this atlas tier'?

How do I solve: 'MongoError: $where is not allowed in this atlas tier' when using MongoDB Atlas? Here is my code: async function getEventsTakingPlace(){ const getEventsTakingPlace = await event.find({$where: function() { return ((this.eventTime < new Date()) && (new Date(this.eventTime).getTime()+ 100*60000 > new Date().getTime() ) ) }}, function(err, events){ if(err){ return err; }else { return events; } }); return getEventsTakingPlace }; when I use my local mongoDB instance it works but once i start ising mongoAtlas I get the error: $where is not allowed in this atlas tier. How do I solve

2022-01-17 03:45:06    分类:问答    mongodb   mongodb-query