Reputation: 332
I have created a seed for my meteor app with the following code.
if (Years.find().count() === 0) {
for (y = 1905; y < 2015; y++ ) {
Years.insert({
year: y
});
}
}
I then want to reference the _id or year field in find statements within my app.
What I have seen is that the seed creates what seems to me to be an array of arrays
I have tried to run a query that look like this.
Years.find().fetch()
And this returned the array of arrays
Years.find().fetch();
Array[110]
[0 … 99]
0: Object
_id: "hPXZuc54irJQ78mQ5"
year: 1905
__proto__: Object
1: Object
_id: "Z7nuSCRA8W7nn9j92"
year: 1906
__proto__: Object
[100 … 109]
length: 110
I then tried to use $elemMatch from searching stackoverflow.
Years.find({year: {$elemMatch:{ year: "1988" } } });
And this is what I got back
LocalCollection.Cursor {collection: LocalCollection, sorter: null, _selectorId: undefined, matcher: Minimongo.Matcher, skip: undefined…}
_selectorId: undefined
_transform: null
collection: LocalCollection
_docs: LocalCollection._IdMap
_idParse: function (id) { // 1079
_idStringify: function (id) { // 1054
_map: Object
2BiEQnaFo37MfXgZo: Object
2LXuNkpH3uq27Wobw: Object
Is there a better way to seed perhaps? or do I need to change the way I query?
Upvotes: 1
Views: 208
Reputation: 44298
Your collection is actually made of a document per year.
the .fetch()
makes an array of all the documents
you've also inserted your years as numbers, so you need to query with a number
your elemmatch query doesn't really make sense, its trying to query the "year" array in each document, which doesn't exist. a year is a number, not an array by the way you have defined it.
so you can just do
Years.findOne({year: 1988})
To see what's going on with mongo ( where the data is stored) go to your mongo console
in your project folder
meteor mongo
then you can do
meteor:PRIMARY> db.years.find().pretty()
and you should see a bunch of documents like :-
{ "year" : 1905, "_id" : "XEyPJnQeFpzxwWnWT" }
{ "year" : 1906, "_id" : "eT6Ctz5yQpZ5FpDuE" }
{ "year" : 1907, "_id" : "7jwaxumxT73X9fAK2" }
{ "year" : 1908, "_id" : "aoMJhnNBaMBv2u9ip" }
{ "year" : 1909, "_id" : "Doyta3rnXE5NvwFTY" }
{ "year" : 1910, "_id" : "uHxAsxcyi9GNYeTbN" }
{ "year" : 1911, "_id" : "BG7AmXxdMRjDLG7cp" }
Upvotes: 1