1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
import mongoose, { Schema, Types } from "mongoose";
require("mongoose-long")(mongoose);
const userSchema = new Schema({
id: String,
});
const messageSchema = new Schema({
id: String,
content: String,
});
const message = mongoose.model("message", messageSchema, "messages");
const user = mongoose.model("user", userSchema, "users");
messageSchema.virtual("u", {
ref: user,
localField: "id",
foreignField: "id",
justOne: true,
});
messageSchema.set("toObject", { virtuals: true });
messageSchema.set("toJSON", { virtuals: true });
async function main() {
const conn = await mongoose.connect("mongodb://localhost:27017/lambert?readPreference=secondaryPreferred", {
useNewUrlParser: true,
useUnifiedTopology: false,
});
console.log("connected");
// const u = await new user({ name: "test" }).save();
// await new message({ user: u._id, content: "test" }).save();
const test = await message.findOne({}).populate("u").exec();
// @ts-ignore
console.log(test?.toJSON());
}
main();
|