Last active
April 4, 2018 02:50
-
-
Save lakamsani/d5e13580b5e36cf399aafaad95e15d92 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// run it with: /usr/local/bin/node -r babel-register mongoos-mongodb3.6.js | |
import mongoose from 'mongoose' | |
import TestModel from './TestMongooseModel' | |
(async () => { | |
/* | |
https://docs.mongodb.com/manual/reference/connection-string/#urioption.authSource | |
https://github.com/Automattic/mongoose/issues/6041 | |
*/ | |
// works | |
//const url = 'mongodb://<user>:<pass>@test-shard-<1>.mongodb.net:<port>,test-shard-<2>.mongodb.net:<port>,test-shard-<3>.mongodb.net:<port>/<appDB>?ssl=true&authSource=admin' | |
// does not work | |
const url = 'mongodb+srv://<user>:<pass>@<host>.mongodb.net/admin?authSource=<appDB>' | |
//const mongoAutoIndex = (process.env.MONGODB_AUTOINDEX || 'false') | |
const mongoAutoIndex = true | |
// http://mongodb.github.io/node-mongodb-native/2.2/api/MongoClient.html#connect | |
var options = {autoIndex: mongoAutoIndex, autoReconnect: true, reconnectTries: Number.MAX_VALUE, poolSize: 10 } | |
const connectStatus = await mongoose.connect(url, options) | |
// confirm we can save a mongoose mapped object to MongoDB | |
let modelInstance = new TestModel({ | |
clientId: `Test2${Date.now()}`, | |
clientSecret: 'A Client secret' | |
}) | |
// FAILS on this line. | |
await modelInstance.save() | |
const modelInstances = await TestModel.find() | |
console.log(modelInstances) | |
process.exit(0) | |
})().catch(err => { | |
console.log(err.stack) | |
process.exit(1) | |
}) | |
// TestMongooseModel: seprate file | |
const mongoose = require('mongoose') | |
const Schema = mongoose.Schema | |
var TestMongooseModelSchema = new Schema({ | |
clientId: {type: String, index: true, unique: true, required: true}, | |
clientSecret: String, | |
redirectUri: String | |
}) | |
TestMongooseModelSchema.index({clientId: 1, clientSecret: 1}, { unique: true, background: true, sparse: true }) | |
let TestMongooseModel = mongoose.model('test_mongoose_model', TestMongooseModelSchema) | |
module.exports = TestMongooseModel |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment