[英]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控制台和后端服務器mongo shell上都編寫了以下查詢,它也返回了空集合。
Javascript控制台
Meteor.users.find({"profile.birthday": {$lt: new Date()}})
服務器MongoDB Shell
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:00:00Z”,因此它應該達到此記錄,但是我所能擁有的只是一個空集合。
有人告訴我使用ISODate(),我嘗試了以下命令在MongoDB Shell中確實起作用,同時在Meteor幫助函數中產生了ISODate Undefined。
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中構建選擇器,然后將其放入查找中,而這將無法工作:-
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}});
}
}
}
服務器/ 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;
}
}
});
邏輯是這樣的,實際上我有很多下拉列表,每個下拉列表都將構成查詢的一部分,並且我使用實例反應變量來設置查詢,以便每當更改時,meteor都會再次執行meteor.find。 除此生日外,每個下拉列表都可以正常工作。 有人知道出什么事了嗎?
暗示:
在mongodb shell中嘗試以下查詢格式。
db.users.find(
{"profile.birthday":{
$gte: ISODate(fromAge),
$lt: ISODate(toAge)
}
})
fromAge
和toAge
應該是正確的格式。
從另一個論壇我得到了答案。
有關詳細信息,請參閱https://forums.meteor.com/t/meteor-db-find-with-date-inside-a-json-as-selector-not-working/12892/9
簡而言之,我只是在查詢中新建了一個Date對象
query[DBPrefix] = {
$lte: new Date(fromDate),
$gte: new Date(toDate)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.