0

I'm trying to insert this object into a mongo DB collection. I've tried a lot of ways and haven't gotten any results. I was wondering if anyone here could help me. The main problem is when passing the array of items into the key items.

The JSON File is similar to this:

{
  'code': 'iuhuilknlkn',
  'description': 'nllksnd',
  'currency': 'Mxn',
  'items': [
    {
      'item': {
        '_id': {
          '$oid': '60065d253ef6d468ced3603f'
        },
        'code': '2',
        'description': '22',
        'currency': 'Mxn',
        'MU': 'Hr',
        'sellingPrice': 1,
        'buyingPrice': 3,
        'supplier': None,
        'cid': '5fbd81b32b325e5ca15fe5c9',
        'itemAmount': 1,
        'itemProductPrice': 1,
        'itemAmountPrice': 1
      }
    },
    {
      'item': {
        '_id': {
          '$oid': '6011c18883a280ae0e5b8185'
        },
        'code': 'prb-001',
        'description': 'prueba 1 artículo',
        'currency': 'Mxn',
        'MU': 'Ser',
        'sellingPrice': 100.59,
        'buyingPrice': 12,
        'supplier': None,
        'cid': '5fbd81b32b325e5ca15fe5c9',
        'itemAmount': 1,
        'itemProductPrice': 100.59,
        'itemAmountPrice': 100.59
      }
    }
  ],
  'price': 101.59
}

and the code I'm using right now is the following:

productsM.insert({
               'code':newP['code'],
               'description':newP['description'],
               'currency' :newP['currency'],
               'items' : [([val for dic in newP['items'] for val in 
                         dic.values()])],
               'price' : newP['price'],
               'cid': csHe })

The error I get is the next one:

key '$oid' must not start with '$'
My Work
  • 2,143
  • 2
  • 19
  • 47
  • Take a look at https://stackoverflow.com/questions/26938598/mongodb-oid-vs-objectid – Joe Feb 09 '21 at 19:33
  • You can try using [mongoimport](https://docs.mongodb.com/manual/reference/program/mongoimport/index.html) command-line tool. The JSON data is in the format of _extended json_, mongoimport is the right tool for that. – prasad_ Feb 10 '21 at 06:11

1 Answers1

-1

Your error is quite clear. Simply removing the $ from the $oid keys in your dictionary/json will resolve the issue. I don't think you can have a $ in key names since they are reserved for operators such as $in or $regex.

I removed the $ from the $oid keys and it worked like a charm. All I did was the following:


data = {
  "code": "iuhuilknlkn",
  "description": "nllksnd",
  "currency": "Mxn",
  "items": [
    {
      "item": {
        "_id": {
          "oid": "60065d253ef6d468ced3603f"
        },
        "code": "2",
        "description": "22",
        "currency": "Mxn",
        "MU": "Hr",
        "sellingPrice": 1,
        "buyingPrice": 3,
        "supplier": None,
        "cid": "5fbd81b32b325e5ca15fe5c9",
        "itemAmount": 1,
        "itemProductPrice": 1,
        "itemAmountPrice": 1
      }
    },
    {
      "item": {
        "_id": {
          "oid": "6011c18883a280ae0e5b8185"
        },
        "code": "prb-001",
        "description": "prueba 1 artículo",
        "currency": "Mxn",
        "MU": "Ser",
        "sellingPrice": 100.59,
        "buyingPrice": 12,
        "supplier": None,
        "cid": "5fbd81b32b325e5ca15fe5c9",
        "itemAmount": 1,
        "itemProductPrice": 100.59,
        "itemAmountPrice": 100.59
      }
    }
  ],
  "price": 101.59
}

db.insert(data)