Node.js Mongoose是否修改字符串数据?

Node.js Mongoose是否修改字符串数据?,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,摘要 将图像数据存储为base64编码的字符串是可行的,但是当查看Mongo中的内容时,我保存的原始字符串仅是结束持久化字符串的前半部分。因此,查询不起作用 详细信息 考虑以下模式: var mongoose = require('mongoose') , Schema = mongoose.Schema; var imageSchema = new Schema({ mime: String, bin: String, uses : [{type: Schema

摘要

将图像数据存储为base64编码的字符串是可行的,但是当查看Mongo中的内容时,我保存的原始字符串仅是结束持久化字符串的前半部分。因此,查询不起作用

详细信息

考虑以下模式:

var mongoose = require('mongoose')
  , Schema = mongoose.Schema;

var imageSchema = new Schema({
    mime:  String,
    bin: String,
    uses : [{type: Schema.Types.ObjectId}]
});

module.exports = mongoose.model('Image', imageSchema);
bin
字段中,我正在存储base64图像数据。所以这个字符串可能很大(但是当它也只有40k左右时,测试失败)

更新

我已经做了进一步的分析,更新后的试块显示了更好的结果:

var buffer = getImageFromFileSystem();
var bin = buffer.toString('base64');
var i = new Image({
    mime : 'image/jpeg',
    bin : bin
});
i.save(function(err, image){
    if (err)
        console.log(err);
    console.log("=== original? "+(image.bin === bin)); //outputs true
    console.log("now find it by id and then compare again")
    Image.findOne({
        _id : image._id
    }, function(err, img) {
        if (err)
            console.log('crap error');
        if (img) {
            console.log('found one!: '+img.id+', lets compare... ');
            console.log("===? "+(img.bin === bin)); //outputs true again!
        }
    });
    //OK so clearly all is well, so let search by the bin str
    Image.findOne({
        bin : bin
    }, function(err, img) {
        if (err)
            console.log('crap error');
        if (!img) {
            console.log("none found") // we hit this... what the heck???
    });
因此,我决定在数据库中查找
bin
值。在这里,我看到它的值是我保存的原始
bin
字符串加上至少两倍的附加字符。因此,我的原始base64字符串是存储在MongoDB中的值开头的子字符串。因此,当我执行
findOne({bin:myBinValue})
时,这不起作用,因为比较是在Mongo内部完成的。但是,如果我从Mongo检索
bin
值,然后进行比较,它似乎会修剪回我的原始值,然后在我的应用程序中,javasript比较是相同的!什么给你!?我没有这个字段的索引

> db.images.getIndexes()
[
        {
                "v" : 1,
                "key" : {
                        "_id" : 1
                },
                "ns" : "mydb.images",
                "name" : "_id_"
        }
]

字符串长度的唯一限制是MongoDB文档的16MB限制。但是在
bin
上放置
unique
索引是行不通的,因为索引值必须小于1024字节,所以这可能会造成麻烦。我还尝试将
bin
属性更改为just
bin:String
,结果相同。我是不是遗漏了什么?实际上,console.log上可能有限制吗?您需要显式地删除索引(在代码中或控制台中),而不仅仅是将其从架构中删除。请参阅更新的帖子。