本文介绍了将多个BSonDocuments从文件插入MongoDB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含以下JSON信息的文本文件:

I have a text file with the following JSON info:

{
  "name" : "system-admin",
  "description" : "Administrador de Sistema",
  "id" : 9,
  "roleid" : 6
}{
  "name" : "rs-promo-server",
  "description" : "Servidor de Promo",
 "id" : 10,
 "roleid" : 7

}

我正在尝试使用以下代码将多个BSONDocuments插入Mongo :

I'm trying to insert multiple BSONDocuments into Mongo with the following code:

BsonDocument doc = BsonDocument.Parse(Json); //Json is a string variable that holds the file    content            
var collection = BaseDatos.GetCollection<BsonDocument>(nombreCollection);
collection.Insert(doc);     

代码工作正常,但只插入文件的第一个BSON文档。
有没有办法在同一个集合中插入文本文件的所有文件?

The code works fine but it inserts only the first BSON document of the file.Is there a way to insert into the same collection all the Documents of the text file?

感谢您给我的任何帮助。

Appreciate any help you may give me.

推荐答案

BsonDocument.Parse(str)只解析一个JSON文档。你在字符串中有更多的。

BsonDocument.Parse(str) parses only one JSON doc. You have more of them in the string.

基本上你有两个选择:

如果你可以改变输入文件的格式,以便每个JSON在单独的行上(您需要最后一行空行以通知最后一个obj),您可以使用readline模块:

If you can change the format of the input file so that each JSON is on separate line (you'll need last empty line to be notified about last obj), you can use readline module:

var fs = require('fs')
var file = 'data.d'

var readline = require('readline')
  , rl = readline.createInterface({
        input : fs.createReadStream(file)
      , output: process.stdout
      , terminal: false
    })

    rl.on('line', function (line) {

      console.log('line: ')
      var obj = {}
      try {
        obj = JSON.parse(line)
      } catch (e) {
        console.log('error: ' + e)
        return
      }
      console.log(obj)
    })

如果你不能改变格式,我害怕你必须解析你的字符串自己,找到JSON对象的边界并单独解析它们(假设你没有嵌套的JSON对象,否则会更复杂):

If you can't change the format, I afraid you have to parse the string yourself, find boundaries of JSON objects and parse them individually, something like that (assuming you don't have nested JSON objects, otherwise it will be more complicated):

var fs = require('fs')
var file = 'data.d'
var s = fs.readFileSync(file, 'utf8')
var start = s.indexOf('{')
var end = s.indexOf('{', start + 1)
while (start !== -1 && end !== -1) {
  var oneObjStr = s.substring(start, end)
  var oneObj = JSON.parse(oneObjStr)
  // save obj into db
  console.log(oneObj)
  start = end
  end = s.indexOf('{', start + 1)
}
if (start !== -1) {
  var lastOne = JSON.parse(s.substring(start, s.length))
  // save the last obj into db
  console.log(lastOne)
}

我认为,您还可以将对象存储到数组中并将该数组存储在一个mongodb插入中:

I think, you can also store objects into an array and store that array in one mongodb insert:

var objs = []
objs.push(oneObj)
// save into db in batch
// db.coll.insert(objs, ...) 

(只是为了澄清 - data.d包含您的输入文件)

(just to clarify - data.d contains your input file)

这篇关于将多个BSonDocuments从文件插入MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 12:20