bson

How could I write a BsonDocument object into a file, and read it again, using Java

丶灬走出姿态 提交于 2019-12-10 20:59:31
问题 I want to output a BsonDocument to a file system, not to mongodb, and read it again using java. But current BasicBSONEncoder only support BSONObject which is the old version.How can I encode BsonDocument into binary? Or is there any way I can convert BsonDocument into BSONObject? 回答1: The eaziest is to convert the BSON to JSON and use java.io API's to write these data to files. The reverse would be to use Reader API's (BufferedReader or FileReader) to read these back and convert to BSON. Now

Facebook user_id as MongoDB BSON ObjectId?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 17:33:37
问题 I'm rebuilding Lovers on Facebook with Sinatra & Redis. I like Redis because it doesn't have the long (12-byte) BSON ObjectIds and I am storing sets of Facebook user_ids for each user. The sets are requests_sent, requests_received, & relationships, and they all contain Facebook user ids. I'm thinking of switching to MongoDB because I want to use it's geospatial indexing. If I do, I'd want to use the FB user ids as the _id field because I want the sets to be small and I want the JSON responses

Python, Convert bson output of mongodump to array of json objects (dictionaries)

本小妞迷上赌 提交于 2019-12-10 17:16:47
问题 I have dumped a mongodb collection using the mongodump command. The output is a dump directory which has these files: dump/ |___coll.bson |___coll.metadata.json How can I open the exported files to a array of dictionaries that work in python? I tried the following and none worked: with open('dump/coll.bson', 'rb') as f: coll_raw = f.read() import json coll = json.loads(coll_raw) # Using pymongo from bson.json_util import loads coll = loads(coll_raw) ValueError: No JSON object could be decoded

Change BSON field value (C++)

我是研究僧i 提交于 2019-12-10 16:43:46
问题 I'm testing BSON as a wire protocol, and I need to be able to change some values on a already created BSONObj... the problem is I can't find a way to do this other than recreating the BSONObj from scratch! Any tips? 回答1: I received a reply from Dwight Merriman, from 10gen: generaly they are immutable -- the common usage is to serialize/deserialize from a cpp object of your choice. See also $set and $inc but those are server-side. So... there we have it :) 回答2: As it was said there is no way

BSON object size of document retrieved from DB

别等时光非礼了梦想. 提交于 2019-12-10 16:06:45
问题 Mongo shell have the bsonsize() method to get BSON size of a given document retrieved from DB. Is there any way of getting the same using PyMongo driver? I have found the bson module in the documentation but it is not fully clear to me how to use it to get the size of a document retrieved from DB. 回答1: Based on @SSDMS suggestion (thanks!) the following can be used: len(bson.BSON.encode(document)) I have tested with a couple of documents in my DB, comparing the result at mongo shell and with

How to save Timestamp type value in MongoDb | Java

泪湿孤枕 提交于 2019-12-10 12:58:07
问题 From Java driver, I want to save a document that looks like below json in MongoDb { "ts" : Timestamp(1421006159, 4)} Options I tried. Option 1: Map doc= new HashMap(1); doc.put("ts", new BSONTimeStamp()); It results in the below not required format {"ts" : { "_inc" : 0, "_class" : "org.bson.types.BSONTimestamp" }} Option 2: doc.put("ts",new Timestamp(new Date().getTime())); it results in : {"ts" : ISODate("2015-01-12T05:36:43.343Z")} 回答1: I used the following with the default mongodb-java

MongoDB Scala Driver - Rendering BSON Documents

大憨熊 提交于 2019-12-10 09:57:35
问题 We currently have a Type-Safe query language at work, this custom DSL allows us to easily write database queries that are interpreted and converted into Mongo Queries. We recently swapped over from Casbah to the new Mongo Scala Driver and rewrote our interpreter. I am however having some issues when dealing with optional values. This is an example query: dao.headOption(Order.id === orderId.some) The type stored on the Order Object is an Option so we lift the provided id into an option as well

Why is it called BSON?

强颜欢笑 提交于 2019-12-10 02:45:01
问题 So BSON is JSON serialized right? {"hello": "world"} → "\x16\x00\x00\x00\x02hello\x00 \x06\x00\x00\x00world\x00\x00" But why is it called Binary Json? What does binary stands for? I always tend to associate binary with 10101010101. But the BSON serialization format above wasn't in 101010101010 form. Could someone explain for me what the Binary here means so I understand why it's called Binary JSON? 回答1: It's binary as opposed to text. Whereas JSON is human-readable text, BSON is binary data

import error: no module named bson

不羁的心 提交于 2019-12-10 01:12:13
问题 I'm trying to import json_util in my environment file: from bson import json_util I get this import error: no module named bson . I tried to pip install and uninstall pymongo and bson - but nothing seemed to help. I found out that the bson package is included in pymongo so I installed it explicitly and then I received an EPOCH_AWARE import error. Currently, only pymongo is installed. It works when I force the virtual environment by using this line: #subprocess.Popen(['/home/.virtualenvs

MongoDB can't update document because _id is string, not ObjectId

最后都变了- 提交于 2019-12-09 11:31:03
问题 I'm doing a rest api to exchange data between a mongo database and a web app. These data are json formatted. I have a trouble when it comes to update a document: cannot change _id of a document. As a matter of fact, in my JSON the _id of the doc is stored as a string and deserialized as a string. Whereas it is stored as an ObjectID in mongo. This explains why mongo raises an error. In mongo: _id: ObjectId('51051fd25b442a5849000001') In JSON: _id:"51051fd25b442a5849000001" To avoid this I