Petrov
Petrov

Reputation: 4250

Meteor queries not working with Meteor.Collection.ObjectID

I'm having a lot of trouble getting query results for certain collections in Meteor. I have set

idGeneration : 'MONGO'

in the collection definitions, and in the mongo shell these collections look like this :

the document i want, call it documentW (from CollectionA) = { 
    "_id" : ObjectId("7032d38d35306f4472844be1"), 
    "product_id" : ObjectId("4660a328bd55247e395edd23"), 
    "producer_id" :  ObjectId("a5ad120fa9e5ce31926112a7") }

documentX (from collection "Products") = {
    _id : ObjectId("4660a328bd55247e395edd23")
}

documentY (from collection "Producers") = {
    _id : ObjectId("a5ad120fa9e5ce31926112a7")
}

If i run a query like this in Meteor

CollectionA.findOne({ product_id : documentX._id, producer_id : documentY._id})

I'm expecting to get my documentW back... but I get nothing.

When I run this query in the mongo shell

db.collectiona.find({ product_id : ObjectId("4660a328bd55247e395edd23"), producer_id : 
ObjectId("a5ad120fa9e5ce31926112a7") })

I get my documentW back no problem.

Of course in Meteor if I call

console.log(documentX._id)

I get this

{ _str : "4660a328bd55247e395edd23" }

Anyone have any ideas what is going on here ? I have tried all kinds of things like

Meteor.Collection.ObjectID(documentX._id._str)

but the search still returns empty...

Running the latest 0.7.0.1 version of Meteor...

Upvotes: 1

Views: 503

Answers (1)

sbking
sbking

Reputation: 7680

I don't know if this answers your question, but I can't put this code in a comment. This code is working for me, trying to follow what I believe you are trying to do:

Products = new Meteor.Collection("products", {
  idGeneration: "MONGO"
});
Producers = new Meteor.Collection("producers", {
  idGeneration: "MONGO"
});
CollectionA = new Meteor.Collection("a", {
  idGeneration: "MONGO"
});

Products.insert({
  foo: "bar"
});
Producers.insert({
  fizz: "buzz"
});

var documentX = Products.findOne();
var documentY = Producers.findOne();

CollectionA.insert({
  product_id: documentX._id,
  producer_id: documentY._id
});

var documentW = CollectionA.findOne({
  product_id: documentX._id,
  producer_id: documentY._id
});

console.log(documentW); // This properly logs the newly created document

This is on 0.7.0.1. Do you see anything in your code that diverges from this?

Upvotes: 2

Related Questions