Nestaed model also create index on wrapper model #4872

Open
olsynt opened this Issue Jan 4, 2017 · 1 comment

Projects

None yet

2 participants

@olsynt
olsynt commented Jan 4, 2017 edited

Hi everyone,
I'v tried to create some model that one of his props types is schema of another one.
The nested model has unique index.
I want that index to be kept in the model but not to be also on the wrapper model.
I'v created simpe code to demonstrate it.

Here we can see the Car has index on name and year, both.
And i want to remove that index from the wrapper model, Provider.
The first thought I got is that I'm probably not doing something well, dropping index or event the schema architecture.
I can see before the create that the index is been dropped but after that i see it again.
Any suggestion please?

I run the latest mongoose, node 6.9.2 and mongo 3.4.0

'use strict';

const http = require('http');
const mongoose = require('mongoose');
const express = require('express');
var app = express();
var server = http.createServer(app);
server.listen(process.env.PORT, null, function() {
  console.log('Express server listening on %d, in %s mode');
});
mongoose.connect(process.env.MONGO, () => {});
const Car = new mongoose.Schema({
  name: String,
  year: String
});
Car.index({'name': 1, 'year': 1}, {unique: true});
const CarModel = mongoose.model('Car', Car);
const Provider = new mongoose.Schema({
    name: String,
    car: CarModel.schema
});
const ProviderModel = mongoose.model('Provider', Provider);
const cars = [{
  name: 'Honda',
  year: '1988'
}, {
  name: 'Toyota',
  year: '2003'
}];
mongoose.connection.collections['providers'].dropIndex('car.name_1_car.year_1', (err, status) => {
  console.log(err, status);
  CarModel.create(cars[0])
  .then((a) => {
    console.log(a);
  });
});
@varunjayaraman
Collaborator

Could you please use the issue template? I'm a bit confused about what you expected the behavior to be and what the actual behavior was.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment