Home > Net >  Mongo invalid document for insert: keys cannot begin with "$": "$date"
Mongo invalid document for insert: keys cannot begin with "$": "$date"

Time:05-08

I'm working on a script that is moving some documents from one collection to another, so if a document met the needed criteria I'm inserting it into another collection and removing it from the original collection. And one document failed with the error invalid document for insert: keys cannot begin with "$": "$date" because it has an field like:

   "rerender_at" : {
        "$date" : {
            "$numberLong" : "1485872000000"
        }
    },

But if mongo does not allow to insert of fields with $ how it's possible that the document has that data already??? Or if it's possible to insert, how can I do it???

Referring to this topic it's not allowed to insert

CodePudding user response:

{
    "$date" : {
        "$numberLong" : "1485872000000"
}

is BSON date in canonical extended JSON format https://mongodb.com/docs/manual/reference/mongodb-extended-json/#mongodb-bsontype-Date

You cannot insert it directly but need to convert it to MongoDB\BSON\UTCDateTime

  • Related