landland
landland

Reputation: 2147

How do I use ObjectID from a mongoimport?

I use a mongoimport to import a bunch of large csv files into a meteor collection, however when they do the insertions the _id values are ObjectID, whereas meteor uses string ids. There's a small blurb on ObjectIDs in the meteor docs here but I don't really understand what I am supposed to do. For example, using Iron Router I have a single route like so

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
                }
            });
        }

but the url of the route is of type object and looks like http://localhost:3000/profiles/ObjectID(%22530845da3621faf06fcb0802%22). It also doesn't return anything and the page renders blank. Here's the publication.

Meteor.publish('singleProfile', function(id, userId) {
    return Profiles.find({
        _id: id,
        userId: userId,
        forDel: {
            $ne: true
        }
    });
});

I guess my question is, how am I supposed to use ObjectIDs so that the routes use just the string portion of the ObjectID, and how do I return the data properly?

Update: I've managed to get the ObjectID out of the url by changing the link to the view from <a href="{{pathFor 'profileView'}}" class="profile-details">Details</a> to <a href="/profiles/{{_id._str}}" class="profile-details">Details</a> so the url is now http://localhost:3000/profiles/530845da3621faf06fcb0802. Unfortunately, the page still renders blank, and I am not sure if that's because of the way I am subscribing, publishing, or finding the collection item.

Upvotes: 2

Views: 1608

Answers (1)

Serkan Durusoy
Serkan Durusoy

Reputation: 5472

Summing up the comment thread as an answer:

The string part of the ObjectID can be obtained by simply calling ._str on the id as

id._str

You can also craft an ObjectID from a hex string by using

new Meteor.Colletion.ObjectID(hexstring)

So when you access your route using <a href="/profiles/{{_id._str}}" class="profile-details">Details</a> you can craft your find like:

Profiles.findOne({
  _id: new Meteor.Collection.ObjectID(this.params._id)
});

Generally speaking, when working with ObjectID's, you will find yourself needing some antipatterns to convert from string to objectId or vice versa, so a utility like the following will come in handy:

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

and

IDAsObjectId = this._id._str ? this._id :  new Meteor.Collection.ObjectID(this._id)

Please also take a look at github.com/meteor/meteor/issues/1834 and groups.google.com/forum/#!topic/meteor-talk/f-ljBdZOwPk for pointers and issues around using ObjectID's.

Upvotes: 2

Related Questions