由于选择器不起作用,json中包含日期的Meteor db.find
Meteor db.find with Date inside a json as selector not working
我阅读了互联网上的许多资源,并遵循了许多关于如何在特定日期范围内找到集合的示例,但都没有成功。我的代码如下:-
var fromAge = 18;
var toAge = 22;
Meteor.users.find({:profile.birthday":{$lt: new Date(new Date().setYear(new Date().getFullYear() - fromAge)), $gt: new Date(new Date().setYear(new Date().getFullYear() - toAge))}).fetch();
当我做一个简单的Meteor.users.find().fetch()时,上面的查询返回了一个空集合,它确实返回了下面的一条记录:-
{"_id": 123452345, "profile.birthday": ISODATE("1975-10-22T00:00:00Z")}
为了调试,我还在javascript控制台和后端服务器mongoshell上编写了以下查询,它也返回了空集合。
Javascript控制台
Meteor.users.find({"profile.birthday": {$lt: new Date()}})
服务器MongoDB外壳
db.users.find{"profile.birthday": {$lt: new Date()}})
我现在不知道。如果有人能启发我就太好了。
--更新--
我尝试了下面的查询,但没有返回任何
db.users.find({"profile.birthday":{$lt: new Date(new Date().setYear(new Date().getFullYear() - 18)).toISOString()}})
我在javascript控制台中键入了命令
new Date(new Date().setYear(new Date().getFullYear() - 18)).toISOString()
它确实告诉我
"1997-11-06T16:35:28.844Z"
在我的数据库中,生日的记录应该是"1975-10-22T00:00Z",因此它应该达到这个记录,但我所能拥有的只是一个空集合。
有人告诉我要使用ISODate(),我尝试了下面的命令,它在MongoDB Shell中确实有效,而它在我的Meteor助手函数中产生了未定义的ISODate。
db.users.find({"profile.birthday":{$lt: ISODate("1977-11-06")})
阅读了其他一些资源(使用MongoDB和Nodejs插入和查询日期),了解到我应该使用Date而不是ISODate,因为ISODate是MongoDB内部的,Meteor将帮助进行Date到ISODate的转换。然后我尝试对查询进行硬编码,它确实运行良好:-
db.users.find({"profile.birthday": {$lt: new Date("1977-11-06T00:00:00.000Z")}})
--更新开始(2015年11月16日)--
它工作得很好,而我的问题是我试图用json构建选择器,然后将其放入find中,但这不起作用:-
selector={"profile.birthday": {$lt: new Date("1977-11-06T00:00:00:000Z"}};
db.users.find(selector);
上面的查询根本没有返回任何结果。
--更新结束(2015年11月16日)--
我这样做是因为我有一个年龄组的下拉列表,并试图通过助手事件来构建查询。我有一个下拉模板如下:-/client/ageRangeDropdown.html
<template name="AgeRangeDropdown">
<div class="btn-group">
Age Group
<select id="ageRangeDropdownList" class="form-control">
<option >Whatever</option>
<option >18-22</option>
<option >23-27</option>
<option >28-32</option>
<option >33-37</option>
<option >38-42</option>
<option >43-47</option>
<option >48-52</option>
<option >53-57</option>
</select>
</div>
{{#each currentCriterias}}
<span class="label label-info">{{criteria}}</span>
{{/each}}
{{#each myUser}}
{{profile.firstName}} , {{profile.lastName}}, {{profile.birthday}}
{{/each}}
</template>
和下面的助手
/client/ageRangeDropdown.js
Template.ageRangeDropdown.events({
'change ageRangeDropdownList': function(e) {
for (var key in target) {
if (!target.hasOwnProperty(key)) {
if (key=='id') {
id = target[key];
}
}
}
var value = target.options[target.selectedIndex].value;
if (value!='Whatever') {
currentCriterias.push({id: "profile.birthday", criteriaValue: value});
}
var query = [];
for (var i = 0; i < items.length; i++) {
var itemObject = {};
var fromAge = currentCriterias[i]['criteriaValue'].substr(0,2);
var toAge = currentCriterias[i]['criteriaValue'].substr(3,2);
itemObject['profile.birthday'] = {
$gt: new Date(new Date().setYear(new Date().getFullYear() - fromAge)),
$lt: new Date(new Date().setYear(new Date().getFullYear() - toAge))
}
query.push(itemObject);
}
var selector = [];
selector.push({$or: query});
Template.instance().selector.set({$or: selector});
return false;
})
Templage.ageRangeDropdown.created = function() {
var instance = this;
instance selector = new RactiveVar([]);
instance.autorun(function() {
var selector = instance.selector.get();
if (typeof selector != 'undefined' && Object.keys(selector).length > 0) {
var subscription = instance.subscribe('searchUser', selector);
}
});
instance.myUser = function() {
var selector = instance.selector.get();
if (typeof selector != 'undefined' && Object.keys(selector).length > 0) {
return Meteor.users.find(selector, {fields: {profile: 1}});
}
}
}
server/serverpublish.js
Meteor.publish("searchUser", function(selector){
if (!this.userId) {
console.log("Cannot search User when you haven't logged in.");
return null;
} else {
if (typeof selector != 'undefined' || Object.keys(selector).length > 0) {
var user=Meteor.users.find(selector, {fields: {profile: 1}});
return user;
} else {
console.log("won't publish all user profiile when no selector is given");
return null;
}
}
});
逻辑是这样的,实际上我有很多下拉列表,每个下拉列表都将构成查询的一部分,我使用实例反应变量设置查询,这样无论何时更改,流星都会再次执行流星查找。每个下拉列表都很好,除了这个生日,它是一个日期。有人知道出了什么问题吗?
提示:
在mongodb shell中尝试以下查询格式。
db.users.find(
{"profile.birthday":{
$gte: ISODate(fromAge),
$lt: ISODate(toAge)
}
})
CCD_ 1和CCD_。
从另一个论坛我得到了答案。
有关详细信息,请参阅https://forums.meteor.com/t/meteor-db-find-with-date-inside-a-json-as-selector-not-working/12892/9
简而言之,我只是在查询中新建了一个日期对象,就像一样
query[DBPrefix] = {
$lte: new Date(fromDate),
$gte: new Date(toDate)
}
- 比较从函数和生成的日期对象
- Meteor如何接收HTTP请求
- Meteor-将选定窗体中的对象添加到集合中
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 如何在JavaScript中延长当前日期
- Meteor-在一段时间内解锁模板(按日期)
- 如何根据创建日期对对象进行排序,以插入Meteor的高图表
- 在 Meteor 中,如何在给定年份、月份、日期的集合中找到记录
- 如何使用 jquery: globalize - meteor 中的日期模块
- 无法在 Meteor 中提交 JQuery 日期选取器值
- Meteor:如何使用Momentjs在Mongodb中添加和查询日期
- TypeError:无法读取属性'日期'在Meteor应用程序中使用tsega:bootstrap3日期
- 在Meteor Handlebars bracers中设置日期格式{{timestamp}}
- 如何对用户中的两个日期对象作出反应's在Meteor中的配置文件
- 仅对时间使用Meteor自动成型bs日期时间选择器
- 由于选择器不起作用,json中包含日期的Meteor db.find
- 在Meteor AutoForm SimpleSchema中验证日期值
- 为什么Meteor中的服务器渲染模板会在前一天解释日期