mongodb-query

Matching ObjectId to String for $graphLookup

久未见 提交于 2019-12-17 02:51:19
问题 I'm trying to run a $graphLookup like demonstrated in print bellow: The objective is to, given a specific record (commented $match there), retrieve it's full "path" throught immediateAncestors property. As you can see, it's not happening. I introduced $convert here to deal with _id from collection as string , believing it could be possible to "match" with _id from immediateAncestors records list (which is a string ). So, I did run another test with different data (no ObjectId s involved): db

Check if every element in array matches condition

£可爱£侵袭症+ 提交于 2019-12-17 02:38:19
问题 I have a collection of documents: date: Date users: [ { user: 1, group: 1 } { user: 5, group: 2 } ] date: Date users: [ { user: 1, group: 1 } { user: 3, group: 2 } ] I would like to query against this collection to find all documents where every user id in my array of users is in another array, [1, 5, 7]. In this example, only the first document matches. The best solution I've been able to find is to do: $where: function() { var ids = [1, 5, 7]; return this.users.every(function(u) { return

Overflow sort stage buffered data usage exceeds internal limit

给你一囗甜甜゛ 提交于 2019-12-17 02:34:32
问题 Using the code: all_reviews = db_handle.find().sort('reviewDate', pymongo.ASCENDING) print all_reviews.count() print all_reviews[0] print all_reviews[2000000] The count prints 2043484 , and it prints all_reviews[0] . However when printing all_reviews[2000000] , I get the error: pymongo.errors.OperationFailure: database error: Runner error: Overflow sort stage buffered data usage of 33554495 bytes exceeds internal limit of 33554432 bytes How do I handle this? 回答1: You're running into the 32MB

Toggle boolean value of subdocuments

自作多情 提交于 2019-12-14 04:12:18
问题 i am trying to toggle the boolean value inside an array which is a collection of objects, problem is that field is being triggered for two both objects inside array, and i want to toggle it for a one object only. Document: "Invitation" : [ { "__v" : 0, "ID" : ObjectId("54afaabd88694dc019d3b628"), "__t" : "USER", "_id" : ObjectId("54b5022b583973580c706784"), "Accepted" : false }, { "__v" : 0, "ID" : ObjectId("54af6ce091324fd00f97a15f"), "__t" : "USER", "_id" : ObjectId(

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

这一生的挚爱 提交于 2019-12-14 03:52:48
问题 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" }, {

MongoDB Query, sort then take nth document for group

最后都变了- 提交于 2019-12-14 03:39:42
问题 I am trying to compose a MongoDB query... My aim is to retrieve a list of player records by country sorted by rating and then return only the nth rated player for each country (so the top rated, or the 3rd best rated etc). I have achieved part I: db.getCollection('players').find( { event: 'open' }).sort({ country: 1, rating: -1 }); Update Here is sample of two countries with three players for each, ordered by rating: Team One: { "id" : 400041, "name" : "Adams Michael", "rating" : 2727,

how to connect mongoDB to server?

戏子无情 提交于 2019-12-14 03:29:08
问题 I am trying to get data from mongoDB while connecting to server .i inserted one value in mongoDB like this > use abc switched to db abc > db.ac.insert({name:"naveen"}) WriteResult({ "nInserted" : 1 }) > show collections ac system.indexes And try to get that value like this var express=require('express'); var app =express(); var MongoClient = require('mongodb').MongoClient; var assert = require('assert'); app.get('/',function(req,res){ console.log("Connected to server."); MongoClient.connect

How to retrieve values from an array in MongoDB using C#

霸气de小男生 提交于 2019-12-14 03:13:33
问题 Below in the code that retrieves the elements in the form of a BsonArray. I just want to fetch the numeric value from the array and use that value to calculate the sum. var fields = "secondary.amount"; foreach (var document in collection.FindAllAs<BsonDocument>().SetFields(fields)) { foreach (string name in document.Names) { BsonElement element = document.GetElement(name); Console.WriteLine("{0}", element.Value); } } I tried converting the bson element to an int64, int32, double and then use

Possible to do partial update, but complete document upsert in mongodb?

寵の児 提交于 2019-12-14 03:04:30
问题 Using mongoDB, I want to update a specific field conditionally (update field if existing value is less than update). However, if the document doesn't exist, I want the whole document to be inserted. What previously worked: I have previously used a combination of $max (to set the field) and $setOnInsert (to set the rest of the document), which did what i wanted. This prevented future updates from decrementing the given field. For example, having the following document schema: { _id: [ObjectId]

MongoDB: Select element from array based on another property in the document

给你一囗甜甜゛ 提交于 2019-12-14 02:25:33
问题 I have a MongoDB collection with documents of the following structure (non-interesting bits left out): { displayFieldId: "abcd", fields: [ { fieldId: "efgh", value: "cake" }, { fieldId: "abcd", value: "cheese" }, .... ], .... } I would like to run a query on this collection to fetch only the element in the fields array which fieldId matches the document's displayFieldId . The result of the query on the document above should thus be: { fields: [ { fieldId: "abcd", value: "cheese" } ], .... } I