These are chat archives for Automattic/mongoose

Jan 2018
Alex Steffes
Jan 28 2018 16:01
const TweetSchema = new Schema({
  tweet_id:{type:String, required: true, unique: true}
  post_text: String
mongoose.model('Tweet', TweetSchema)

const UserSchema = new Schema({
  liked_tweets: [{type: Stirng, ref: "Tweet.tweet_id"}]
is something like this possible? mytweet_id is indexed in mongo(tweet_id_1). I need to handle duplicates when new tweets are liked buy a user but already exist in my db. I currently have
liked_tweets: [{ type : Schema.Types.ObjectId, ref: ‘Tweet’}]
but I don’t get the ObjectId returned of an existing tweet after E11000 duplicate key error because of unique:true. Any ideas?
Juha Lindstedt
Jan 28 2018 16:03
Why can’t you just use _id?
Alex Steffes
Jan 28 2018 16:08
because right now after my insertMany() any duplicates return this error
E11000 duplicate key error index: mycollection.tweets.$tweet_id_1 dup key: { : \"952961370349334529\" }"
but they do not return ObjectId of existing Tweet so I cant add that ObjectId to user's liked_tweetsarray says:

Note: ObjectId, Number, String, and Buffer are valid for use as refs

Koby Picker
Jan 28 2018 23:29
Hi! I'm trying to build a script to seed my database. The API I'm building has a function which creates matches between candidates and employers, so once the data has been inserted with mongoose-seed, I need to run the matching algorithm. Here's the problem I'm facing: the matching algorithm runs asynchronously, so the main thread exits before it's finished, which closes the database connection. Any thoughts on how to keep that resource open? Thanks! I'm a Node beginner so the answer might be really obvious.
Jan 28 2018 23:54
how are you handling the async stuff? with promises?
@JKobyP ^