Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/447.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用字符串类型数据在mongo中查找日期间_Javascript_Node.js_Mongodb - Fatal编程技术网

Javascript 使用字符串类型数据在mongo中查找日期间

Javascript 使用字符串类型数据在mongo中查找日期间,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,我在mongodb上的数据存储方式如下: { "_id" : { "$oid" : "5385a437084ea4734b03374f" }, "linea" : 1, "egunak" : [ { "fetxa" : "2014/05/26", "turnoak" : [ { "turno" : 1,

我在mongodb上的数据存储方式如下:

{ 
    "_id" : { "$oid" : "5385a437084ea4734b03374f" },
    "linea" : 1, 
    "egunak" : [ 
        { 
            "fetxa" : "2014/05/26", 
            "turnoak" : [ 
                 { 
                     "turno" : 1,
                     "ordenes" : [ 
                         { "ref" : "3CI00001" },
                         { "of" : "OF000d013" }
                     ]
                 }, 
                 { 
                     "turno" : 2, 
                     "ordenes" : [ 
                         { "ref" : "3CI00001" }, 
                         { "of" : "112-2233" }, 
                         { "ref" : "3CI0-0001" },
                         { "ref" : "666" }, 
                         { "ref" : "33" }, 
                         { "ref" : "3355" },
                         { "ref" : "345" },
                         { "ref" : "1234" } 
                     ] 
                 },
                 { 
                     "turno" : 3,
                     "ordenes" : [
                         { "ref" : "3CI00001" },
                         { "ref" : "12" }
                     ]
                 }
            ]
        }, 
        { "fetxa" : "2014/05/27" },
        {
            "fetxa" : "2014/05/28",
            "turnoak" : [
                { 
                    "turno" : 1,
                    "ordenes" : [
                        { "ref" : "3CI0-0001" },
                        { "of" : "OF200013" }
                    ]
                },
                { 
                    "turno" : 2,
                    "ordenes" : [
                        { "ref" : "3CI00001-" },
                        { "of" : "OF232233" },
                        { "of" : "OF289977" }
                    ]
                },
                { 
                    "turno" : 3,
                    "ordenes" : [
                        { "ref" : "3CI00001" },
                        { "of" : "OF200-000" },
                        { "ref" : "3CI00001" },
                        { "of" : "OF200000" },
                        { "ref" : "3CI00001" }
                    ]
                }
            ]
        }, 
        { "fetxa" : "2014/05/29" },
        { "fetxa" : "2014/05/30" },
        { "fetxa" : "2014/05/31" },
        { "fetxa" : "2014/06/01" }
    ]
},
{ 
    "_id" : { "$oid" : "5385a448084ea4734b033750" },
    "linea" : 2,
    "egunak" : [
        {
            "fetxa" : "2014/05/26",
            "turnoak" : [
                { 
                    "turno" : 2,
                    "ordenes" : { "ref" : "123" } 
                }
            ]
        },
        {
            "fetxa" : "2014/05/27",
            "turnoak" : [
                {
                    "turno" : 1,
                    "ordenes" : [
                        { "ref" : "3CI00002" },
                        { "of" : "2OF000013" }
                    ]
                },
                { 
                    "turno" : 2,
                    "ordenes" : [
                        { "ref" : "3CI00001" },
                        { "of" : "2OF2233" },
                        { "ref" : "3CI00001" },
                        { "ref" : "999" }
                    ]
                },
                {
                    "turno" : 3,
                    "ordenes" : [
                        { "ref" : "3CI00001" }
                    ]
                }
            ]
        },
        { 
            "fetxa" : "2014/05/28",
            "turnoak" : [
                {
                    "turno" : "2",
                    "ordenes" : { "ref" : "66" }
                }
            ]
        },
        {
            "fetxa" : "2014/05/29",
            "turnoak" : [
                { 
                    "turno" : 1,
                    "ordenes" : [
                        { "ref" : "3CI00001" },
                        { "of" : "2OF200013" }
                    ]
                },
                { 
                    "turno" : 2,
                    "ordenes" : [
                        { "ref" : "3CI00001" },
                        { "of" : "2OF232233" },
                        { "ref" : "3CI00001" }
                    ]
                },
                { 
                    "turno" : 3,
                    "ordenes" : [
                        { "ref" : "3CI00001" },
                        { "of" : "2OF200000" },
                        { "ref" : "3CI00001" },
                        { "of" : "2OF200000" }, 
                        { "ref" : "3CI00001" }
                    ]
                }
            ]
        },
        { "fetxa" : "2014/05/30" },
        { "fetxa" : "2014/05/31" },
        { "fetxa" : "2014/06/01" }
    ]
}
现在我想查找两个日期之间的数据,但数据是以字符串形式存储的,我如何实现这一点?或者我需要以日期格式存储数据

这是我在nodejs上的persist函数:

exports.save = function(req, res){
    db.open(function(err, db) {
        if(!err) {
            var data = req.body;
            var BSON = mongo.BSONPure;
            var o_id = new BSON.ObjectID(data._id);

            db.collection('test').update({'_id': o_id}, { $set :{ egunak: data.egunak } }, {safe:true, multi:false, upsert:false}, function(e, result){
                if (e) console.log(e)
                res.send((result===1)?{msg:'success'}:{msg:'error'})
            })

        } else {
            onErr(err, function(){
                console.log(err);
            });
        }
    });
};

其中$set值是我的JSON。如何存储日期数据?

虽然最好以日期格式存储日期,但如果仍然需要解决方法,可以使用date.parse(date)函数以毫秒为单位获取值。我在mongodb 2.4.8中尝试了以下操作,两种方法都很好

var d = Date.parse("March 21, 2012"); 
var d = Date.parse("2014/03/21");
现在“d”将以毫秒为单位包含数据,您可以使用它查找两个日期之间的数据。

  • 你可以随时使用
stringify(js_对象);//这将js对象转换为字符串格式的对象

在发送数据之前,以及

JSON.parse(对象采用字符串格式);//这会将字符串格式的对象转换回js对象

在收到数据之后。阅读更多信息

  • 您应该以日期类型存储datetime

  • 然后您可以像下面这样“选择”:

    db.collection('Datuak').find(
      {"egunak.fetxa": {"$gte": new Date(1979, 1, 1), //date type here
                      "$lt": new Date(2222, 12, 31)}
      }
    )
    

是的,您应该以日期格式存储日期。你也不说日期。那是什么?这份文件上到处都有日期。我在寻找类似于从Datuak选择*的东西,egunak在'01/01/1979'和'31/12/2222'之间。在这种情况下,将是所有的,但我会改变它的三明治组合框。当您通过JSON发送数据时,如何从节点持久化mongo上的数据?例如,关于我的问题的JSON。当您通过JSON发送数据时,如何从节点持久化mongo上的数据?例如,关于我的问题的JSON。当数据最初没有存储为日期时,查找两个日期之间的毫秒是没有帮助的。