I'm following the instructions in this link https://anthonychu.ca/post/cosmos-db-mongoose-discriminators/ to store multiple document types in a single collection within an Azure Cosmos DB (Mongo API).
I'm using mongoose 5.2.10, nodejs 8.12
I do have models with fields set at "unique: true". E.g.
Here is my Base schema:
const mongoose = require('mongoose')
const baseOptions = {
discriminatorKey: '__type',
collection: 'data'
}
module.exports = mongoose.model('Base', new mongoose.Schema({}, baseOptions))
And here's one of the actual models:
const mongoose = require('mongoose')
const uniqueValidator = require('mongoose-unique-validator')
const Base = require('./_Base')
const roles = require('../lib/auth').roles
const UserSchema = new mongoose.Schema({
displayName: { type : String, default: '' },
email: { type : String, unique : true, required: true },
role: { type : String , default: roles.Users },
tenant: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Tenant'
}
})
UserSchema.plugin(uniqueValidator)
module.exports = Base.discriminator('User', UserSchema)
All my data gets stored in a collection with the name 'data' as indicated in my Base model, but somehow another collection gets created in Cosmos DB named "undefine" which seems to be related to the indexes of my data collection:
Question: How to prevent the creation of this "undefined" collection and get any index-related data to be placed in the same collection as my data?
Thanks
Related
I'm new to MongoDB and I know this might sound silly to many but is there a way(s) where you can create multiple documents from an array of values. I have written some code for this, but is there any other efficient/shorter way of doing that.
tags.map(async tag => {
const tagDoc = await TagModel.create({ tag, postID })
})
Tag Schema -
const tagSchema = new mongoose.Schema({
postID: [{
type: mongoose.Schema.Types.ObjectId,
ref: "Post",
}],
tag: {
type: String,
required: true,
},
}, { timestamps: true })
Case 1 : Storing Tags in Same Collection
in mysql you have to store tags in different table , but in mongodb you can use same collection to store tags as array of string
in your post schema :
const postSchema = new mongoose.Schema({
...
tags: {
type: [String],
required: true,
}
});
when saving post =>
let post = new Post();
...
post.tags = req.body.tags; // ["Tag1","hello","Testing"];
await post.save();
Case 2 : Storing Tags in Different Collection
if you want to store tags in different collection Tag , then you can use Mongoose InsertMany()
let tags = req.body.tags ; // ["Tag1","hello","Testing"];
tagsArray = tags.map((t)=> { return {postID:post._id , tag:t}});
let tagsSaved = await Tag.insertMany(tagsArray);
console.log('tagsSaved=',tagsSaved);
I am using mongodb with mongoose. And i am wondering if it is possible to have multiple references for an object id attribute in a schema.
I have tried the code underneath and it did not work.
const Schema = new Schema({
refrens: {
type: ObjectId,
// this did not work
ref: [
"Post",
"Account"
],
required: true
}
});
I know it is possible to remove the ref attribute (field, key) and then all object ids are valid but i want certain object ids to be valid, the object ids of the Post and Account model.
const Schema = new Schema({
refrens: {
// This will allow all different types of object ids to be the value
type: ObjectId,
required: true
}
});
Look like refPath is what you need. You can do something like this:
const Schema = new Schema({
refrens: {
type: ObjectId,
refPath: 'onModel',
required: true
},
onModel: {
type: String,
required: true,
enum: ['Post', 'Account']
}
});
I am working on trying to connect a Node/Express server to an existing MongoDB Database/Collection. I have already successfully connected to the database. However, I am having a tremendously difficult time setting up my models/schema to query.
The MongoDB is MongoDB Atlas and has one collection with over 800,000 documents. The name of the single collection is "delitosCollection".
I have tried the following to with no success:
var CrimeData = mongoose.model('DelitosCollection', new Schema({}),'delitosCollection');
mongoose.connection.on('open', function(ref){
console.log("connected to the mongo server");
CrimeData.find({}, (err,results) => {
if(err){
console.log("ERROR")
throw err
}
console.log("results: ", results.length)
} )
});
I know the connection is working as I am receiving the console.log with no errors. However, results.length is returning 0 when it should be over 800,000. Have spent way too many hours on this.
Create an empty schema for each collection you want to use
and then create a model to be used in your project
the model take 3 parameter
1)name of the model
2)schema name
3)collection name ( from mongodb atlas)
like that
const mongoose = require('mongoose');
mongoose.connect('mongodb uri')
const userSchema = new mongoose.Schema({});
const User = mongoose.model('User', userSchema, 'user');
then you can use the model normally
User.find({})
connection to mongo db
// Connect to mongoDB
mongoose.connect('mongodb://localhost/[yourDbName]',{useNewUrlParser:true})
.then(function(){
console.log('mongoDB connected');
})
.catch(function(){
console.log('Error :');
})
after that you will have to create your schema and then only you can query the database
create your schema like this
// Crimes Schema
const CrimeDetailsSchema= new Schema({
first_name: {
type: String,
required: true
},
last_name: {
type: String,
required: true
},
email: {
type: String,
required: true
}
});
const Profile = module.exports = mongoose.model('delitosCollection', CrimeDetailsSchema, 'delitosCollection');
after that create your queries
you can get an idea about that in mongoose documentation here
You can refer to the answer given below, just pass an empty object in schema
like db.model('users', new Schema({}))
I have made my model, it has this Schema:
const CompanySchema = new Schema({
companyName: {
type: String,
required: [true,'the name of the companies working on the game are missing.']
},
companyAge: {
type: Number,
required: [true,'the age of the company is missing.']
},
companyDeveloper:[{
type: Schema.Types.ObjectId,
ref: "developer"
}]
});
I am trying to push a element into the companyDeveloper array like this:
addDev(req,res,next){
const companyId = req.params.id;
const companyDeveloper = ObjectId.fromString(req.body.companyDeveloper);
Company.findById({_id: companyId})
.then((company) => company.companyDeveloper.push({companyDeveloper}))
.then(company => res.send(company))
.catch(next);
}
but I keep getting this error:
"error": "ObjectId is not defined".
Before I tried casting it, I got this error
Cast to ObjectId failed for value
How can I get this function to work?
printscreen
postman call error
The ObjectId class from mongoose is defined on Mongoose.Schema.Types.ObjectId
You can require it in the file where you define addDev
const ObjectId = require('mongoose').Schema.Types.ObjectId
or load mongoose into a global where you initialize your node code so you can access it in any file:
global.Mongoose = require('mongoose')
And then use it in your method:
const companyDeveloper = Mongoose.Schema.Types.ObjectId.fromString(req.body.companyDeveloper);
i have created model for the mongo collection like below. but it was giving me the collection output which saved in mongoDB.
var mongoose = require('mongoose'),
Schema = mongoose.Schema({
name: {
type: String
},
age: {
type: Number
},
})
module.exports = mongoose.model('container', Schema);}
But later when i changed the last line of the code which is
"module.exports = mongoose.model('container', Schema);"
to
"module.exports = mongoose.model('container', Schema, 'container');"
it worked properly. I check the mongoose document they say to use the previous line, then why didn't it worked.
your problem seems to be from using "Schema" as a variable name
var ContainerSchema = new mongoose.Schema({
...
});
and exporting
module.exports = mongoose.model("Container", ContainerSchema);
would work.