mongodb : limit the possible values of a number field - mongodb

I want to limit the choices for a field in mongo:
units : { type: Number, default: 1 },
But I would like to add this constraint: something like
authorized values: [1, 10, 100, 1000]

You are clearly using mongoose for which there is an enum type validator available:
var mySchema = new Schema({
"units": { "type": Number, "default": 1, "enum": [1,10,100,1000] }
})

There is no enum in MongoDB since there is no enum in json / bson, so yeah, you can't, you only can control entered values using your programming language (ex: fire an exception when an unexpected value is entered).

Mongoose's enum validator only works with string types, but you can use a custom validator to do this:
units : {
type: Number,
default: 1,
validate: {
validator: function (value) {
return [1, 10, 100, 1000].indexOf(value) !== -1;
},
message: 'Value must be one of 1, 10, 100, 1000'
}
}

Related

What's a good way to look up list of enumerated values?

I have a collection of case with a field named status (integer) whose valid values are 0, 1, 2, 4 and 8, representing "New", "WIP", "Solved", "Canceled" and "Closed" respectively.
So, in mongoose syntax, it might be like:
const caseSchema = new Schema({
createdOn: Date,
subittedBy: String,
status: Number,
...
});
const statusSchema = new Schema({
value: Number,
description: String
});
Is this a good way to organize the data? How do I make a query to retrieve cases with the status field properly filled with the description?
It is one way to do it sure. You could do the query by using $lookup. It would look something like this:
db.getCollection('<YourCasesColName>').aggregate([
{ $match : { 'status' : 1 } }, // or { $in: [1,2,3] },
{
$lookup: {
from: '<YourStatusColName>',
localField: 'status',
foreignField: 'value',
as: 'statusDoc',
}
}
])
Another way is to add a reference to the actual status via ObjectId so that instead of numbers in the cases you would be storing references to the actual Status objects and in this way have a better referential integrity. However you would still need to do similar query to get both in one shot. So here is what I am talking about:
const caseSchema = new Schema({
createdOn: Date,
subittedBy: String,
status: { type: mongoose.Schema.Types.ObjectId, ref: 'Status' },
// ^ now your status hows reference to exactly the type of status it has
});
const statusSchema = new Schema({
value: Number,
description: String
});
So the actual data would look like this:
// Statuses
[{
_id: <StatusMongoObjectID_1>,
value: 1,
description: 'New'
},{
_id: <StatusMongoObjectID_2>,
value: 2,
description: 'New'
}]
// Cases
[{
_id: <MongoObjectID>,
createdOn: '<SomeISODate>',
subittedBy: '<SomeString>',
status: <StatusMongoObjectID_1>
},
{
_id: <MongoObjectID>,
createdOn: '<SomeISODate>',
subittedBy: '<SomeString>',
status: <StatusMongoObjectID_2>
}]

MongoDB Document Validation in Meteor?

How would one approach doing this (https://docs.mongodb.com/v3.2/core/document-validation/):
db.createCollection( "contacts",
{ validator: { $or:
[
{ phone: { $type: "string" } },
{ email: { $regex: /#mongodb\.com$/ } },
{ status: { $in: [ "Unknown", "Incomplete" ] } }
]
}
} )
In this:
// database.js
import { Mongo } from 'meteor/mongo';
export const Test = new Mongo.Collection('Test');
Thanks
you first need to define your schema in meteor.
Lists.schema = new SimpleSchema({
name: {type: String},
incompleteCount: {type: Number, defaultValue: 0},
userId: {type: String, regEx: SimpleSchema.RegEx.Id, optional: true}
});
This example defines a schema with a few simple rules:
We specify that the name field of a list is required and must be a
string.
We specify the incompleteCount is a number, which on insertion is
set to 0 if not otherwise specified.
We specify that the userId, which is optional, must be a string that
looks like the ID of a user document.
It’s pretty straightforward to validate a document with a schema. We can write:
const list = {
name: 'My list',
incompleteCount: 3
};
Lists.schema.validate(list);
In this case, as the list is valid according to the schema, the validate() line will run without problems. If however, we wrote:
const list = {
name: 'My list',
incompleteCount: 3,
madeUpField: 'this should not be here'
};
Lists.schema.validate(list);
Then the validate() call will throw a ValidationError which contains details about what is wrong with the list document.

MongoError unknown top level operator: $set

When I do this:
return scores.updateQ({_id:score._id},
{
$set:{"partId":partId,"activityId":activityId},
$unset:{topicType:'',topicId:'',courseId:''}
},
{strict:false})
Where partId and activityId are variables, defined elsewhere,
I get
{ name: 'MongoError',
message: 'unknown top level operator: $set',
index: 0,
code: 2,
errmsg: 'unknown top level operator: $set' }
This answer says
"The "unknown top level operator" message means that the operator has
to appear after the field to which it applies."
But the documentation says you can do it the way I'm doing it
So maybe there's something else wrong?
The problem is that you're using the syntax for the wrong update method. You should be using this method's syntax, assuming that scores is a document.
return scores.updateQ({
$set: { "partId": partId, "activityId": activityId},
$unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });
Also, in Mongoose, it uses $set by default, so this should be equivalent:
return scores.updateQ({
partId: partId,
activityId: activityId,
$unset: { topicType: '', topicId: '', courseId: ''}
},
{ strict: false });
EDIT:
My assumption is that scores is a document (an instance of the Model):
var schema = new Schema({});
var Scores = mongoose.model('Scores', schema);
var scores = new Scores({});
Both Scores.update and scores.update exist, but the syntax is different, which may be what's causing your error. Here's the difference:
// Generic update
Scores.update({ _id: id }, { prop: 'value' }, callback);
// Designed to update scores specifically
scores.update({ prop: 'value' }, callback);
NOTE:
If these assumptions are not correct, include more context in your answer, like how you got there.

How to stop MongoDB to add values to my objects?

I'm writing a little personal project requiring to write into a db and getting content to be displayed with d3js. So I'm using objects containing like {x: "2004-10-02", y:20} etc... Now I found that mongoDB adds y0:0 and even y1: 20 when I change the value of y using $set with Mongoose.
I have this Schema:
var projectSchema = mongoose.Schema({
key : String,
description: String,
values: [{x: Date, y: Number, comment: String}]
});
It works fine. I can insert documents (incomplete model) sending a request with this body:
{
name: "name of the Project",
description: "insert descritption"
}
and process the request with this:
var newProjectDetails = req.body;
var np = new Project(newProjectDetails);
np.save(function (err) {
if (err) {
return err;
}
else {
console.log("Project saved");
}
});
then I add values to the values array (see above the Schema) and it works fine the first time, but if I try to overwrite an entry in the array I get this a messed object, with extra values that seem to be old values of the y property.
Here's how I add an entry into the values array:
Project.findByIdAndUpdate(req.body._id,
{ $set: { values: req.body.values } },
function(err, tank){
if(err){
res.send(err);
}
res.send(tank);
})
But then when I fetch the content I got a strange issue inside the array:
I get this object in the console:
{
"x": "2014-10-12T00:00:00.000Z",
"y": 20,
"comment": "doanjas dfgixGHWXV",
"_id": "543a96c3a402c9ad17528d52"
}
and this in RestWebServiceClient extention in Chrome:
{
"x": "2014-10-12T00:00:00.000Z",
"y": 20,
"comment": "doanjas dfgixGHWXV",
"_id": "543a96c3a402c9ad17528d52"
}
But inside the console, when I iterate through the collection with Angular I get this:
{ _id: "543a96c3a402c9ad17528d52",
comment: "doanjas dfgixGHWXV",
series: 0,
size: 20,
x: "2014-10-12T00:00:00.000Z",
y: 20,
y0: 0,
y1: 20
}
This is driving me crazy, should I map the array and get rid of this, why would I do that extra work?

MongoDB: How to define a schema?

So I have an application that uses MongoDB as a database. The application makes use of a few collections.
When and how should I go about defining the "schema" of the database which includes setting up all the collections as well as indexes needed?
AFAIK, you are unable to define empty collections in MongoDB (correct me if I am wrong, if I can do this it will basically answer this question). Should I insert a dummy value for each collection and use that to setup all my indexes?
What is the best practice for this?
You don't create collections in MongoDB.
You just start using them immediately whether they “exist” or not.
Now to defining the “schema”. As I said, you just start using a collection, so, if you need to ensure an index, just go ahead and do this. No collection creation. Any collection will effectively be created when you first modify it (creating an index counts).
> db.no_such_collection.getIndices()
[ ]
> db.no_such_collection.ensureIndex({whatever: 1})
> db.no_such_collection.getIndices()
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "test.no_such_collection",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"whatever" : 1
},
"ns" : "test.no_such_collection",
"name" : "whatever_1"
}
]
Create empty collection
This is how you could create empty collection in MongoDB using build in interactive terminal:
db.createCollection('someName'); // create empty collection
Just you don't really have to, because as someone pointed before, they will get created in real time once you start to interact with the database.
MongoDB is schema-less end of story, but ...
You could create your own class that interacts with mongo Database. In that class you could define rules that have to fulfilled before it can insert data to mongo collection, otherwise throw custom exception.
Or if you using node.js server-side you could install mongoose node package which allows you to interact with database in OOP style (Why bother to reinvent the wheel, right?).
Mongoose provides a straight-forward, schema-based solution to model
your application data. It includes built-in type casting, validation,
query building, business logic hooks and more, out of the box.
docs: mongoose NPM installation and basic usage
https://www.npmjs.com/package/mongoose
mongoose full documentation http://mongoosejs.com
Mongoose use example (defining schema and inserting data)
var personSchema = new Schema({
name: { type: String, default: 'anonymous' },
age: { type: Number, min: 18, index: true },
bio: { type: String, match: /[a-zA-Z ]/ },
date: { type: Date, default: Date.now },
});
var personModel = mongoose.model('Person', personSchema);
var comment1 = new personModel({
name: 'Witkor',
age: '29',
bio: 'Description',
});
comment1.save(function (err, comment) {
if (err) console.log(err);
else console.log('fallowing comment was saved:', comment);
});
Wrapping up ...
Being able to set schema along with restriction in our code doesn't change the fact that MongoDB itself is schema-less which in some scenarios is actually an advantage. This way if you ever decide to make changes to schema, but you don't bother about backward compatibility, just edit schema in your script, and you are done. This is the basic idea behind the MongoDB to be able to store different sets of data in each document with in the same collection. However, some restriction in code base logic are always desirable.
As of version 3.2, mongodb now provides schema validation at the collection level:
https://docs.mongodb.com/manual/core/schema-validation/
Example for create a schema :
db.createCollection("students", {
validator: {
$jsonSchema: {
bsonType: "object",
required: [ "name", "year", "major", "address" ],
properties: {
name: {
bsonType: "string",
description: "must be a string and is required"
},
year: {
bsonType: "int",
minimum: 2017,
maximum: 3017,
description: "must be an integer in [ 2017, 3017 ] and is required"
},
major: {
enum: [ "Math", "English", "Computer Science", "History", null ],
description: "can only be one of the enum values and is required"
},
gpa: {
bsonType: [ "double" ],
description: "must be a double if the field exists"
},
address: {
bsonType: "object",
required: [ "city" ],
properties: {
street: {
bsonType: "string",
description: "must be a string if the field exists"
},
city: {
bsonType: "string",
description: "must be a string and is required"
}
}
}
}
}
}
})
const mongoose = require("mongoose");
const RegisterSchema = mongoose.Schema({
username: {
type: String,
unique: true,
requied: true,
},
email: {
type: String,
unique: true,
requied: true,
},
password: {
type: String,
requied: true,
},
date: {
type: Date,
default: Date.now,
},
});
exports.module = Register = mongoose.model("Register", RegisterSchema);
I watched this tutorial.
You have already been taught that MongoDB is schemaless. However, in practice, we have a kind of "schema", and that is the object space of the object, whose relations a MongoDB database represents. With the ceveat that Ruby is my go-to language, and that I make no claims about exhaustiveness of this answer, I recommend to try two pieces of software:
1. ActiveRecord (part of Rails)
2. Mongoid (standalone MongoDB "schema", or rather, object persistence system in Ruby)
Expect a learning curve, though. I hope that others will point you to solutions in other great languages outside my expertise, such as Python.
1.Install mongoose:
npm install mongoose
2. Set-up connection string and call-backs
// getting-started.js
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
//call-backs
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
// we're connected!
});
3. Write your schema
var kittySchema = new mongoose.Schema({
name: String
});
4. Model the schema
var Kitten = mongoose.model('Kitten', kittySchema);
5. Create a document
var silence = new Kitten({ name: 'Tom' });
console.log(silence.name); // Prints 'Tom' to console
// NOTE: methods must be added to the schema before compiling it with mongoose.model()
kittySchema.methods.speak = function () {
var greeting = this.name
? "Meow name is " + this.name
: "I don't have a name";
console.log(greeting);
}
enter code here
var Kitten = mongoose.model('Kitten', kittySchema);
Functions added to the methods property of a schema get compiled into the Model prototype and exposed on each document instance:
var fluffy = new Kitten({ name: 'fluffy' });
fluffy.speak(); // "Meow name is fluffy"