0

I have a model with the a datetime field

user_since = db.DateTimeField()

When I try to insert a new object of the model into mongo, There is no error. But the write does not succeed.

I printed the object from to_json() and tried to insert it with mongo shell, I get the following error.

field names cannot start with $ [$date] at src/mongo/shell/collection.js:L147

the to_json had this field.

"user_since": {"$date": 1392205572989}

I can't seem to find any pointers on how to solve this.

What is causing the write to fail?

How can I make mongoengine to throw error in case of write failure.? Or at least find out what the error was?

Thanks.

Update: As I found later the real problem is not the datetime field. The details of the problem are in this question MongoEngine Document Object made using from_json doesn't save

Community
  • 1
  • 1
shshank
  • 2,571
  • 1
  • 18
  • 27
  • Can you provide more context around the write? Also, can you disclose the entire model? – Rishi Feb 12 '14 at 13:43
  • Please find the details in this question. http://stackoverflow.com/questions/21731860/mongoengine-document-object-made-using-from-json-doesnt-save – shshank Feb 13 '14 at 06:40

1 Answers1

0

With MongoDB you cannot have '$' at the beginning of the field name.

MongoDB Limits and Thresholds - Restrictions on Field Names:

Field names cannot contain dots (i.e. .), dollar signs (i.e. $), or null characters. See Dollar Sign Operator Escaping for an alternate approach.

Try to name it differently.


edit: According to MongoEngine documentation, you could pass db_name parameter:

db_field – The database field to store this field in (defaults to the name of the field)

Jacek Kaniuk
  • 5,229
  • 26
  • 28