1

I'm trying to understand what exactly collection is and how I can define several schemas to one collection and query it.

For example, I want to define two different schemas for collection "animals".

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var cat= new Schema({
    name: String
});

module.exports = mongoose.model('Cat', cat, 'animals');

and another one

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var dog= new Schema({
    friendliness: Boolean
});

module.exports = mongoose.model('Dog', dog, 'animals');

So how I can get posts from the "animal" collection?

UPD: I'm sorry if I didn't explain my question well. Just updating it with my answer to @laggingreflex:

I'm asking about searching through the collection with two schemas. You can see the same question in the comments to Use more than one schema per collection on mongodb - "Any suggestion how to do methods for searching object of BOTH schemas? for example, I want to find both users and registered users and sort that by date, using just query." That's what I'm asking. ;)

Thanks.

Community
  • 1
  • 1
Jake Blues
  • 1,033
  • 1
  • 9
  • 12
  • http://stackoverflow.com/questions/14453864/use-more-than-one-schema-per-collection-on-mongodb – laggingreflex Oct 21 '14 at 01:30
  • @laggingreflex, I've seen this question and answer. But I'm asking about searching through the collection with two schemas. You can see the same question in the comments - "Any suggestion how to do methods for searching object of BOTH schemas? for example, I want to find both users and registered users and sort that by date, using just query." That's what I'm asking. ;) – Jake Blues Oct 21 '14 at 08:32
  • 2
    **Schema** is just a mongoose wrapper, **Collection**s are what's stored in the database. So since the collection is the same, ***I guess*** you could search using either schema models... – laggingreflex Oct 21 '14 at 10:33
  • @laggingreflex It works... omg! I spent about 2 hours yesterday to find out how to search through the collection and the answer was so close. Of course, Model is just a reference to mongo's collection, but I was confused of different model names. If you formalize your comment as the answer, I will mark it like right answer. Thank you! – Jake Blues Oct 21 '14 at 13:50

0 Answers0