在mongolab中的集合中创建对象时发生错误:BasicBSONList仅可使用数字键,而不能使用:[_id] [英] error when creating object in collection in mongolab: BasicBSONList can only work with numeric keys, not: [_id]

查看:466
本文介绍了在mongolab中的集合中创建对象时发生错误:BasicBSONList仅可使用数字键,而不能使用:[_id]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MongoLab中使用以下JSON(并已对其进行验证):

I'm using the following JSON (and have validated it) for my collection in MongoLab:

[{
    "city": "ACMAR",
    "pop": 6055,
    "state": "AL",
    "_id": "35004"
},
{
    "city": "ADAMSVILLE",
    "pop": 10616,
    "state": "AL",
    "_id": "35005"
},
{
    "city": "ADGER",
    "pop": 3205,
    "state": "AL",
    "_id": "35006"
}]

但我一直收到此错误:BasicBSONList只能使用数字键,而不能使用[_id].那我在做什么错了?

But I keep getting this error: BasicBSONList can only work with numeric keys, not: [_id]. So what am I doing wrong?

推荐答案

我想我可能对问题有一个了解,但是您能详细说明几件事吗?这是您收藏中的文档列表还是较大文档中包含的列表?您到底在哪里得到错误?

I think I may have an idea of what the problem is, but can you elaborate on a few things? Is this a list of documents from your collection or a list that is included in a larger document? Where you are getting the error exactly?

MongoLab文档编辑器仅接收一个文档({}不带[]),目前不支持批量插入.

The MongoLab document editor only takes one document ({} not []), it doesn't support batch inserts at this time.

如果您在代码中遇到此问题,那么通过做一些假设,我可以推测出,如果提供的JSON是数据",那么:

If you're having this problem in code, then by making a few assumptions, I can speculate that if the JSON provided is "data" then:

数据是BasicBSONList(或BasicDBList),无法通过调用.get("somefieldname")进行访问.而是:

data is a BasicBSONList (or BasicDBList), which cannot be accessed by calling .get("somefieldname"). Instead, either:

a)遍历列表中的元素以访问它们:

a) iterate over the elements of the list to access them:

for(BasicDBObject doc : data) { String id = (String) doc.get("_id"); }

b)使用已知列表索引作为中介

b) use a known list index as an intermediary

String _id = (String) ((DBObject)data.get(1)).get("_id"); //or
String _id = (String) ((DBObject)data.get("1")).get("_id");

这是因为在幕后,一个BSONList看起来像这样:

This is because, under-the-hood, a BSONList looks something like:

{
"0": {
    "city": "ACMAR",
    "pop": 6055,
    "state": "AL",
    "_id": "35004"
},
"1": {
    "city": "ADAMSVILLE",
    "pop": 10616,
    "state": "AL",
    "_id": "35005"
},
"2": {
    "city": "ADGER",
    "pop": 3205,
    "state": "AL",
    "_id": "35006"
}
}

让我知道这是否有帮助!

Let me know if this helps!

非常感谢, Eric @ MongoLab

Gratefully, Eric@MongoLab

这篇关于在mongolab中的集合中创建对象时发生错误:BasicBSONList仅可使用数字键,而不能使用:[_id]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆