bson

Querying mongodb from golang using the _id stored in an array

限于喜欢 提交于 2019-12-18 09:13:27
问题 So here is my question. I have an array which are stored the _ids of mongodbs objects. Whats the right way to retrieve them all in one query using the mgo and bson package? So if the array is like that: ids:=["543d171c5b2c12420dd016","543d171c5b2dd016"] How we make the query ? I tried that but I know its wrong. query := bson.M{"_id": bson.M{"$in": ids}} c.Find(query).All() Thanks in advance 回答1: If the documents are stored with string ids, then the code looks correct. The ids look like hex

BSON library for java? [closed]

試著忘記壹切 提交于 2019-12-17 18:52:43
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . We have good support for JSON in java http://blog.locut.us/main/2009/10/14/which-is-the-best-java-json-library.html but what about BSON. What library do you know that provides BSON support for java? It should obviously be efficient in runtime. 回答1: The BSON site is pointing at this If you want to use it from

Can MongoDB and its drivers preserve the ordering of document elements

旧巷老猫 提交于 2019-12-17 16:30:27
问题 I am considering using MongoDB to store documents that include a list of key/value pairs. The safe but ugly and bloated way to store this is as [ ['k1' : 'v1'] , ['k2' : 'v2'], ...] But document elements are inherently ordered within the underlying BSON data structure, so in principle: {k1 : 'v1', k2 : 'v2', ...} should be enough. However I expect most language bindings will interpret these as associative arrays, and thus potentially scramble the ordering. So what I need to know is: Does

Serialize/Deserialize MongoDB Bson document using Official C# driver

隐身守侯 提交于 2019-12-14 02:44:59
问题 I recently started working on MongoDB using the official C# driver for MongoDB. I have the following class/interface definitions: public interface IDependent { int Index { get; set; } string Name { get; set; } } public class Person { int Id { get; set; } string Name {get; set; } IDependent[] Dependents { get; set; } } public class Dependent : IDependent { int Index {get; set; } string Name { get; set; } } Using this class/interface structure, inserting Person record with an array of

Query nested BSON documents with mongo c++ driver

筅森魡賤 提交于 2019-12-13 17:24:55
问题 I have a bsoncxx::document::view bsonObjView and a std::vector<std::string> path that represents keys to the value we are searching for in the BSON document (first key is top level, second key is depth 1, third key depth 2, etc). I'm trying to write a function that given a path will search the bson document: bsoncxx::document::element deepFieldAccess(bsoncxx::document::view bsonObj, const std::vector<std::string>& path) { assert (!path.empty()); // for each key, find the corresponding value

Enforce a type mapping with mgo

懵懂的女人 提交于 2019-12-13 16:54:01
问题 An _id member is not mapped to type ObjectId anymore, when its type is only derived from bson.ObjectId: import ( "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) type CustomId bson.ObjectId type Foo struct { ID1 CustomId `bson:"_id"` // broken ID2 bson.ObjectId // mapped as expected } func main() { session, _ := mgo.Dial("127.0.0.1") coll := session.DB("mgodemo").C("foocoll") doc := Foo{ CustomId(bson.NewObjectId()), bson.NewObjectId(), } coll.Insert(doc) } The _id should have been an ObjectId in

How to fetch a list of values from a Bson document?

点点圈 提交于 2019-12-13 14:25:06
问题 I'm parsing in a Json document that stores a list of type <Country> (which holds the name and code ), using the MongoDB.Net driver. But I'm not sure how to retrieve that list of countries from the Bson document. I stepped through the parsing code, all values are being parsed in to the CountryModel. Then stored in the returned collection var. Googling brought me this solution but it only shows how to return a record by ID not a complete List. I'm wondering if a Lambda overload can be used here

Query where sum of two fields is less than given value

江枫思渺然 提交于 2019-12-13 10:18:34
问题 I am using Go language and MongoDB with mgo.v2 driver and I have struct like type MarkModel struct { ID bson.ObjectId `json: "_id,omitempty" bson: "_id,omitempty"` Name string `json: "name" bson: "name"` Sum int `json: "sum" bson: "sum"` Delta int `json: "delta" bson: "delta"` } I need to find all where is Sum + Delta < 1000 for example. At the moment I load all and then in Go code I filter but I would like to filter on query level. How to make that query ? At the moment I return all with

$geowithin:$polygon doesn't use 2dsphere index

风格不统一 提交于 2019-12-13 04:12:05
问题 I am having a collection of documents like following: { "_id" : ObjectId("5bc15f23d672e9086ca4fbac"), "Location" : { "GeoJson" : { "type" : "Point", "coordinates" : [14.4199254356, 50.0700249768] } } And I have Index created as following { Location.GeoJson2dsphere : "2dsphere" } And now to the problem when I use the $polygon for searching, i've got the results, but query is not using index, so it's slow. Here is the query .find({"Location.GeoJson" : { "$geoWithin" : { "$polygon" : [ [14

How to query nested objects?

怎甘沉沦 提交于 2019-12-13 03:58:02
问题 I have a problem when querying mongoDB with nested objects notation: db.messages.find( { headers : { From: "reservations@marriott.com" } } ).count() 0 db.messages.find( { 'headers.From': "reservations@marriott.com" } ).count() 5 I can't see what I am doing wrong. I am expecting nested object notation to return the same result as the dot notation query. Where am I wrong? 回答1: db.messages.find( { headers : { From: "reservations@marriott.com" } } ) This queries for documents where headers equals