如何过滤重复和空文档,以及使用JavaScript订购MongoDB集合?
获取MongoDB集合中的所有数据非常简单。您只需使用集合实例名称并调用find:如何过滤重复和空文档,以及使用JavaScript订购MongoDB集合?,javascript,meteor,duplicates,mongodb-query,meteor-helper,Javascript,Meteor,Duplicates,Mongodb Query,Meteor Helper,获取MongoDB集合中的所有数据非常简单。您只需使用集合实例名称并调用find: TimeAndSpace = new Mongo.Collection('timeAndSpace'); if (Meteor.isClient) { . . . Template.placesLived.helpers({ places: function () { // this helper returns a cursor of all of th
TimeAndSpace = new Mongo.Collection('timeAndSpace');
if (Meteor.isClient) {
. . .
Template.placesLived.helpers({
places: function () {
// this helper returns a cursor of all of the documents in the collection
return TimeAndSpace.find();
}
});
}
但是我的收藏有一些重复的数据和一些空记录。我如何过滤掉这些?我还想按两个字段排序。我的收藏结构和写作目的如下:
TimeAndSpace = new Mongo.Collection('timeAndSpace');
if (Meteor.isClient) {
Template.addTimeSpaceForm.events({
'submit form': function(event){
event.preventDefault();
var city = event.target.city.value;
var state = event.target.state.value;
var yearin = event.target.yearin.value;
var yearout = event.target.yearout.value;
Meteor.call('insertLocationData', city, state, yearin, yearout);
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
Meteor.methods({
'insertLocationData': function(city, state, yearin, yearout){
TimeAndSpace.insert({
ts_city: city,
ts_state: state,
ts_yearin: yearin,
ts_yearout: yearout
});
}
});
}
我希望先按yearin排序,然后按yearout排序(这样,如果有多个文档具有相同的yearin值(例如1984),它们将按以下顺序返回:
Helena Montana 1984 1984
San Andreas California 1984 1987
(反之亦然)
那么,我如何过滤掉空的和冗余的文档,并按指定的字段对文档进行排序呢
更新
考虑到答案不返回任何数据的问题可能是“city”字段应该是“Tsu city”,而“state”字段应该是“Tsu state”,我尝试了以下方法:
return TimeAndSpace.find(
{ts_city: {$exists: true, ne: ""}, ts_state: {$exists: true, ne: ""}},
{sort: {ts_yearin: 1, ts_yearout: 1}}
);
…但它仍然不返回任何数据
更新2
当我在控制台中输入此命令时:
TimeAndSpace.find(
{ts_city: {$exists: true, ne: ""}, ts_state: {$exists: true, ne: ""}},
{sort: {ts_yearin: 1, ts_yearout: 1}}
);
我得到:
Exception in template helper: Error: Inconsistent operator: {"$exists":true,"ne":""}
at http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1269:15
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at isOperatorObject (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1263:5)
at compileValueSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1459:14)
at http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1439:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1422:5)
at _.extend._compileSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1399:12)
at new Minimongo.Matcher (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1342:27)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:144:20)
XHR finished loading: GET "http://localhost:3000/sockjs/info?cb=8zcc5akr4u".
return TimeAndSpace.find(
{sort: {ts_yearin: 1, ts_yearout: 1}}
);
Uncaught SyntaxError: Illegal return statement
at Object.InjectedScript._evaluateOn (<anonymous>:905:140)
at Object.InjectedScript._evaluateAndWrap (<anonymous>:838:34)
at Object.InjectedScript.evaluate (<anonymous>:694:21)
TimeAndSpace.find(
{ts_city: {$exists: true, ne: ""}, ts_state: {$exists: true, ne: ""}},
{sort: {ts_yearin: 1, ts_yearout: 1}}
);
Uncaught Error: Inconsistent operator: {"$exists":true,"ne":""}
at http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1269:15
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at isOperatorObject (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1263:5)
at compileValueSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1459:14)
at http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1439:9
at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
at compileDocumentSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1422:5)
at _.extend._compileSelector (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1399:12)
at new Minimongo.Matcher (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1342:27)
at new LocalCollection.Cursor (http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:144:20)
模板帮助程序中的异常:错误:不一致的运算符:{“$exists”:true,“ne”:“”}
在http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1269:15
at功能。每个功能。每个功能(http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
在等位面对象上(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1263:5)
在compileValueSelector上(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1459:14)
在http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1439:9
at功能。每个功能。每个功能(http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
在CompiledDocumentSelector上(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1422:5)
在u.extend.\u编译器选择器(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1399:12)
在新的Minimongo Matcher(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1342:27)
在新的LocalCollection.Cursor(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:144:20)
XHR已完成加载:获取“http://localhost:3000/sockjs/info?cb=8zcc5akr4u".
返回TimeAndSpace.find(
{sort:{tsu yearin:1,tsu yearout:1}}
);
未捕获的SyntaxError:非法返回语句
在Object.InjectedScript.\u evaluateOn(:905:140)
在Object.InjectedScript.\u evaluateAndWrap(:838:34)
在Object.InjectedScript.evaluate(:694:21)
找到时间和空间(
{ts_市:{$exists:true,ne:'},ts_州:{$exists:true,ne:'},
{sort:{tsu yearin:1,tsu yearout:1}}
);
未捕获错误:运算符不一致:{“$exists”:true,“ne”:“”}
在http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1269:15
at功能。每个功能。每个功能(http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
在等位面对象上(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1263:5)
在compileValueSelector上(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1459:14)
在http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1439:9
at功能。每个功能。每个功能(http://localhost:3000/packages/underscore.js?0a80a8623e1b40b5df5a05582f288ddd586eaa18:164:22)
在CompiledDocumentSelector上(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1422:5)
在u.extend.\u编译器选择器(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1399:12)
在新的Minimongo Matcher(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:1342:27)
在新的LocalCollection.Cursor(http://localhost:3000/packages/minimongo.js?af9eb9d7447544ca9b839a3dcf7ed2da2209b56c:144:20)
让我们先看简单的,消除空白和排序:
return TimeAndSpace.find(
{ts_city: {$exists: true, $ne: ""}, ts_state: {$exists: true, $ne: ""}},
{sort: {ts_yearin: 1, ts_yearout: 1}}
);
另一方面,重复数据消除可能最好由cron作业来处理,该作业会查找您所关心的重复类型并删除有问题的文档。在查询过程中,这实际上是不可能的,在呈现过程中,这将是一个PIA。我在排序行中添加了第二个“}”,以使其“编译”(并在上面添加了这一点),但现在它什么也不返回…抱歉,忘记了城市和州键上的前缀。我在控制台中添加了err msgs来更新2。是的,在“ne”前面加“$”就可以了-谢谢!