Repository navigation
Expand file tree
/
Copy pathposts.js
More file actions
71 lines (53 loc) · 1.39 KB
/
Copy pathposts.js
File metadata and controls
71 lines (53 loc) · 1.39 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const { default: mongoose } = require("mongoose");
const { Schema } = mongoose;
main()
.then(() => console.log("Connection Successful"))
.catch((err) => console.log(err));
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/relationDemo");
}
const userSchema = new Schema({
username: String,
email: String,
});
const postSchema = new Schema({
content: String,
likes: Number,
user: {
type: Schema.Types.ObjectId,
ref: "User"
},
});
const User = mongoose.model("User", userSchema);
const Post = mongoose.model("Post", postSchema);
const getData = async () => {
let result = await Post.findOne({}).populate("user", "username");
console.log(result);
};
getData();
// const addData = async () => {
// let user = await User.findOne({ username: "rahulkumar" });
// let user1 = new User({
// username: "rahulkumar",
// });
// let post1 = new Post({
// content: "Hello World!",
// likes: 7,
// });
// post1.user = user1;
// await user1.save();
// await post1.save();
// let post2 = new Post({
// content: "Bye-Bye :)",
// likes: 23,
// });
// post2.user = user;
// await post2.save();
// };
// console.log("Data saved");
// };
// addData();
// const del = async () => {
// await Post.findByIdAndDelete("6abfd8eccc66d253b551475e");
// };
// del();