Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.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
Meteor 如何使用mongoimport中的ObjectID?_Meteor - Fatal编程技术网

Meteor 如何使用mongoimport中的ObjectID?

Meteor 如何使用mongoimport中的ObjectID?,meteor,Meteor,我使用mongoimport将一组大型csv文件导入meteor集合,但是当它们插入时_id值是ObjectID,而meteor使用字符串id。在meteor文档中有一个关于ObjectID的小广告,但我真的不明白我应该做什么。例如,使用Iron Router,我有一条这样的单路由 this.route('profileView', { path: '/profiles/:_id', notFoundTemplate: 'notFound', fa

我使用mongoimport将一组大型csv文件导入meteor集合,但是当它们插入时_id值是ObjectID,而meteor使用字符串id。在meteor文档中有一个关于ObjectID的小广告,但我真的不明白我应该做什么。例如,使用Iron Router,我有一条这样的单路由

this.route('profileView', {
        path: '/profiles/:_id',
        notFoundTemplate: 'notFound',
        fastRender: true,
        waitOn: function() {
            return [Meteor.subscribe('singleProfile', this.params._id, Meteor.userId())];
        },
        data: function() {
            Session.set('currentProfileId', this.params._id);
            return Profiles.findOne({
                _id: this.params._id
            }, {
                fields: {
                    submitted: 0
                }
            });
        }
但是路由的url是object类型,看起来像
http://localhost:3000/profiles/ObjectID(%22530845da3621faf06fcb0802%22)
。它也不会返回任何内容,页面呈现为空白。这是出版物

Meteor.publish('singleProfile', function(id, userId) {
    return Profiles.find({
        _id: id,
        userId: userId,
        forDel: {
            $ne: true
        }
    });
});
我想我的问题是,我应该如何使用ObjectID,以便路由只使用ObjectID的字符串部分,以及如何正确地返回数据


更新:通过将视图的链接从
更改为
,我成功地从url中获取了ObjectID,因此url现在是
http://localhost:3000/profiles/530845da3621faf06fcb0802
。不幸的是,页面仍然呈现为空白,我不确定这是否是因为我订阅、发布或查找收藏项目的方式。

总结评论线索作为答案:

ObjectID的字符串部分可以通过简单地调用id上的

id._str
您还可以使用

new Meteor.Colletion.ObjectID(hexstring)
因此,当您使用
访问路线时,您可以像这样精心设计您的发现:

Profiles.findOne({
  _id: new Meteor.Collection.ObjectID(this.params._id)
});
一般来说,在使用ObjectID时,您会发现自己需要一些反模式来从字符串转换为ObjectID,或者从字符串转换为ObjectID,因此下面这样的实用程序会派上用场:

IDAsString = this._id._str ? this._id._str : this._id


还请查看github.com/meteor/meteor/issues/1834和groups.google.com/forum/#!topic/meteor talk/f-ljBdZOwPk,了解有关使用ObjectID的指针和问题。

如果将
this.params.\u id
的所有实例替换为
new meteor.Collection.ObjectID(this.params.\u id),会发生什么情况
?@Cuberto我从Deps重新计算中得到一个错误
异常:错误:用于创建ObjectID的十六进制字符串无效
@land,您只需使用括号中的内容,即
ObjectID(%22530845da3621faf06fcb0802%22)
您需要
530845da3621faf06fcb0802
作为
此.params.id
@Akshat。不走运。当我打印到控制台时,此.params._id为530845da3621faf06fcb0802,但页面仍呈现为空白。发布代码和路由代码在其他方面保持不变。只需在id上调用
。\u str
即可获得objectid的字符串部分。此外,请查看和以查找指针
IDAsObjectId = this._id._str ? this._id :  new Meteor.Collection.ObjectID(this._id)