简体   繁体   English

流星不服从Mongo存在

[英]Meteor disobeys $exists from Mongo

I have two collections, one called villages, and one called cities. 我有两个收藏,一个叫做乡村,另一个叫做城市。 I loop through every village and run it against my other collection to find the nearest city. 我遍历每个村庄,并将其与我的其他收藏进行比较,以找到最近的城市。 Then, I add a field city: cityName to my villages collection. 然后,将一个田地城市:cityName添加到我的村庄集合中。 This will let me know which city each village is close to. 这会让我知道每个村庄都在哪个城市附近。

var cursor = Villages.find({city: {$exists: 0}});

cursor.forEach(function(village) {
    var lat = district.loc.latitude;
    var long = district.loc.longitude;
    var city = Cities.find({ loc: {$near: [lat,long],
                                   $maxDistance: 2}},
                           {fields: {name: 1, _id: 0}},
                           {limit: 1});

    city.forEach(function(nearestCity) {
        Villages.update({name: village.name}, 
                        {$set: {city: nearestCity.name}});
    });

});

Every bit of this worked the first time I ran it. 我第一次运行它时,所有这些都起作用。 Starting from the top of my list it went through and over time added about 40,000 city fields to my villages collection. 从我的清单的顶部开始,它经历了一段时间,随着时间的推移,我的村庄收藏增加了大约40,000个城市田地。 My laptop did computer things, connection was lost, I tried to run it again. 我的笔记本电脑完成了计算机操作,连接丢失,我尝试再次运行它。 Instead of starting where it left off, it begins at the top of the collection again, regardless of the fact that the city field exists in those entries. 而不是从其中断处开始,它再次从集合的顶部开始,无论这些条目中是否存在city字段。

I traced the different values and it still works properly, just not on the right documents. 我跟踪了不同的值,但它仍然可以正常运行,只是没有正确的文档。 When I removed the field from one of the first documents, the count of the cursor went up by 1 until it went down the list and passed over it. 当我从第一个文档之一中删除该字段时,光标的数量增加了1,直到它从列表中滑下并通过了它。 Then the count decreased again. 然后,计数再次下降。

My question is this: Why would a cursor that returns the correct number of documents operate on the wrong ones? 我的问题是:为什么返回正确数量文档的游标会在错误的文档上运行?

The problem was that I did not have all unique names for my documents. 问题是我的文档没有唯一的名称。 Changing it to update based on the _id value fixed my issue. 将其更改为基于_id值进行更新_id我的问题。 The app found (as an example) Wembley CA, saw it was closest to Grande Prairie CA, but then applied that information to whichever of Wembley GB or AU the collection decided to surface for me. 该应用程序找到了(例如)温布利CA,发现它最接近Grande Prairie CA,但是随后将该信息应用于集合决定为我显示的任何温布利GB或AU。 This means everything was working correctly, but it would still find the same documents without city values because it was only giving city values to their homonymous siblings. 这意味着一切正常,但仍会找到没有城市价值的相同文档,因为它只是将城市价值提供给同名兄弟姐妹。 My final script was 我最后的剧本是

var cursor = Villages.find({city: {$exists: 0}});

cursor.forEach(function(village) {
    var lat = village.loc.latitude;
    var long = village.loc.longitude;
    var city = Cities.find({loc: {$near: [lat,long], $maxDistance: 2}}, 
                            {fields: {name: 1, _id: 0}} 
                            ).fetch();

    if(city.length == 0) //If no cities are nearby, I let it pick itself
    {
        city = [{name: village.name}];
    }

    Villages.update({_id: village._id}, {$set: {city: city[0].name}});

});

I used find().fetch() rather than just find() . 我使用的是find().fetch()而不是仅仅使用find() This let me access a single document and its values more easily. 这使我可以更轻松地访问单个文档及其值。 This gave me access to the _id field which I used to reference the appropriate document and solve my problem. 这使我可以访问_id字段,该字段用于引用适当的文档并解决了我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM