Collection.findOne({_id: "stringidsdfdsfdsfds"}) is returns undefined


#1

Hi all,
I’m not new to Meteor, but been away for a couple of weeks due to working on other projects.

I’m now working on a Meteor project using React.

When I do this Collection.find({}).fetch() it returns this

[
  {
       "_id":{"_str":"59d3b91d80f4f5eeb0162634"},
       "title":"My first Post",
       "content":"This is the body of the pst"
  }
]

the only strange thing is the _id field.
but when i do Collection.findOne({_id: "59d3b91d80f4f5eeb0162634" }), it returns undefined. How can I do a .findOne() using the _id string as query parameter?

Thanks


#2

You seem to be using an objectId (although it doesn’t look like a standard MongoDB ObjectId). You should perhaps take a look at how you are writing documents into the database.

However, to answer your specific question, you could do Collection.findOne({"_id._str": "59d3b91d80f4f5eeb0162634" }).


#3

Thanks @robfallows for the response.

I entered the values from the meteor mongo console.

I was able to do this in the end

//subscribed to post in constructor

export default createContainer(props => {
  const id = props.match.params.id;
  const posts = Posts.find({}).fetch();
  const post = posts.filter( ps => {return ps._id._str == id})

  return{
    post: post[0]
  }
}, SinglePost);

This is not how I would normally do it, it should be something like this

export default createContainer(props => {
  const id = props.match.params.id;
  return {
    post: Posts.findOne({_id: id})
  }
}, SinglePost);