Is there a length limit to field values in mongo queries?

后端 未结 1 1446
悲哀的现实
悲哀的现实 2021-01-13 11:28

I\'ve got a single document in a collection:

{
  _id: ObjectId(\"508836afea5cea2ccec11a0d\"),
  created_at: 1348657869.204,
  name: \"Abcdefghijklmnopqrstuvw         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 11:56

    The limit you are encountering is the maximum line buffer size in the mongo shell, which is 4096 bytes as at MongoDB 2.2.1. If you try pasting your example in the mongo shell you should notice that you cannot add any characters beyond the line limit.

    If you execute this query from a language driver you will not have this issue.

    You could also workaround this in the mongo shell by loading the query from a JavaScript file specified on the command line:

     mongo longname.js
    

    Or by creating a long string in the mongo shell programmatically:

    // Longname will be 5000 characters
    var longname = '';
    for (i = 0; i < 200; i++) {
        longname += 'Abcdefghijklmnopqrstuvwx ';
    }
    
    db.foo.insert({
      _id: ObjectId("508836afea5cea2ccec11a0d"),
      created_at: 1348657869.204,
      name: longname
    });
    
    printjson(db.foo.findOne({name: longname}))
    

    0 讨论(0)
提交回复
热议问题