-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
executable file
·59 lines (45 loc) · 1.2 KB
/
index.js
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
const mongoose = require('mongoose')
,Schema = mongoose.Schema
mongoose.connect('mongodb://127.0.0.1:27017')
mongoose
.connection
.once('open', _ => console.log(mongoose.connection.name))
const store = Schema({
// ref: {type: Schema.Types.ObjectId, ref: 'test'},
name: String
})
store.virtual('linkto', {
ref: 'tests',
localField: 'name',
foreignField: 'text',
})
const Schema1 = Schema({
age: Number,
name: String,
text: String,
arr: Array,
// children,
})
const User = mongoose.model('tests', Schema1)
const Store = mongoose.model('refs', store)
// Schema1.pre('update', function (...a) {
// console.log('update')
// })
// User.create({name: 'gg', age: 10}).then(res => {
// return Store.create({name: 'gg'})
// })
// .catch(console.error)
Store.findOne({ name: 'gg' })
.populate('linkto')
.then(res => console.log(res.linkto[0]))
// require('./text.js')
// User.update({name: 'gss'}, { $set: { text: '5' } }).then(console.log)
// User.remove({name:'gs'}).then(res => console.log('remove', res))
// User.find({name: 'gss'}).then(console.log)
// User.create({
// age: 22,
// name: 'gss',
// arr: [1]
// })
// .then(res => console.log('ok', res))
// .catch(console.error)