Skip to content

Instantly share code, notes, and snippets.

@jvik
Created January 23, 2019 14:28
Show Gist options
  • Save jvik/d0eef0d41bb3c2935d316d861c3a1b7d to your computer and use it in GitHub Desktop.
Save jvik/d0eef0d41bb3c2935d316d861c3a1b7d to your computer and use it in GitHub Desktop.
const assert = require('assert');
const mongoose = require('mongoose');
mongoose.set('debug', true);
const GITHUB_ISSUE = `1234`;
const connectionString = `mongodb://localhost:27017/${ GITHUB_ISSUE }`;
const {
Schema,
SchemaTypes
} = mongoose;
run().then(() => console.log('done')).catch(error => console.error(error.stack));
async function run() {
await mongoose.connect(connectionString);
await mongoose.connection.dropDatabase();
const gameSchema = new mongoose.Schema({
gameTitle: String,
gameDescription: String,
}, {
timestamps: true,
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
const gameStatusSchema = new mongoose.Schema({
gameID: { type: mongoose.Schema.Types.ObjectId, ref: "Game"},
gameStatus: Number
}, {
timestamps: true,
toObject: { virtuals: true },
toJSON: { virtuals: true }
});
gameSchema.virtual("gameStatus", {
ref: "GameStatus",
localField: "_id",
foreignField: "gameID",
justOne: true
});
let Game = mongoose.model('Game', gameSchema)
let GameStatus = mongoose.model('GameStatus', gameStatusSchema)
await Game.create({_id: "5c473a872721eb7327377fa2", gameTitle: 'Game1', gameDescription: "GameDescription" });
await GameStatus.create([{ gameID: "5c473a872721eb7327377fa2", gameStatus: 10 }]);
const games = await Game.find({})
.sort([['updatedAt', 'descending']])
.populate("gameStatus")
console.log('GAMES', games);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment