Avoid insertion in mongoDB Without Data - mongodb

How can i avoid the insertion in mongoDB if there is no data. Because every time i use insert command it is creating _id without data.
Example : db.collection_name.insert({})
is creating _id in document. How to avoid it.

You can use mongoose ORM and can define validators like:
var UserSchema = new Schema({
name: { type: String, required: true }
});
Which will generate an error message that looks like:
name:
{ message: 'Validator "required" failed for path name',
name: 'ValidatorError',
path: 'name',
type: 'required' } }

MongoDB 3.2 has schema validation integrated.
So you can just check if "name" property exists, if not then the validation fail and then the Document will be not inserted at all.
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" ] } }
]
}
} )

Related

MongoDB (mongoose): Cast to [string] failed for value

There's something wrong happens when I try to update a document in DB.
Here is Schema:
const prodSchema = new Schema({
name: {
type: String,
required: true,
unique: true
},
description: {
type: [String]
},
})
Then I get some product from elsewhere:
const some_product = axios.get(blah blah)
And update mu document.
Note that I set up a condition for a 'description': if it has a null value, it is updated - else it remains the same:
const newProduct = {
$set: {
name: some_product.name,
description: {
$cond: {
if: {
$eq: [null, '$description']
},
then: [some_product.description],
else: '$description'
}
}
}
}
Go update (I use mongoose):
Product.updateOne({name: some_product.name}, newProduct, {some params})
And I see this:
Cast to [string] failed for value
"[{"$cond":{"if":{"$eq":[null,"$description"]},"then":["Here is some
new description of the prosuct"],"else":"$description"}}]" at path
"description"
I think the point is that the type of description in the schema is array of strings, and 'description' field in the requested 'some_product' is just a string. May be this is the issue.
But how do I solve it?
Many thanks.
var description = []
description.push(some_product.description)
const newProduct = [{
$set: {
name: some_product.name,
description: {
$cond: {
if: {
$eq: [null, '$description']
},
then: description,
else: '$description'
}
}
}
}]
put update operation in [] use update pipeline
for example
model.update({},[{$set: ...}])

MongoDB - remove many from arrays of all existing elements

I have some simple user data. Here is example for one user:
const userSchema = new Schema({
userName: {
type: String,
},
projectsInput: [
{
type: Schema.Types.ObjectId,
ref: "Project",
},
],
projectsHold: [
{
type: Schema.Types.ObjectId,
ref: "Project",
},
],
});
I want by having ProjectId to be able to remove all records from all users that contains it.
if I get the first one
60f02d21159c4b4110f21a32
how I can perform updateMany function for my UserModel?
return UserModel.updateMany(
{
projectsInput: {
$elemMatch: args.projectId,
},
},
{
projectsInput: {
$slice: [projectsInput.$, 1],
},
}
);
})
Here is my code that is not working.
args.projectId = 60f02d21159c4b4110f21a32 (my id for the project I want to delete)
and UserModel is my mongodb Schema for user.
you can use $pull
{
$pull: {
projectsInputs: "123"
}
}

GraphQL Mutation Updating Users Followers with Mongoose/MongodDB - $set is empty error

I have this mutation set up:
followUser: {
type: UserType,
args: {
_id: { type: GraphQLString },
firebaseUid: { type: GraphQLString },
following: { type: new GraphQLList(GraphQLString)},
},
resolve(parentValue, { firebaseUid, _id, following}) {
const update = {
$set: { "following": [firebaseUid] },
$push: { "following": { firebaseUid } }
}
return UserSchema.findOneAndUpdate(
{ _id },
update,
{new: true, upsert: true}
)
}
},
I'm trying to add new followers into my graphql user's collection. My user model:
const UserSchema = new Schema(
{
firebaseUid: String,
following: [{ type: Schema.Types.ObjectId, ref: 'User' }],
followers: [{ type: Schema.Types.ObjectId, ref: 'User' }],
},
{ timestamps: true }
);
module.exports = mongoose.model("User", UserSchema);
So at first, the user doesn't have any followers, so it won't have that field yet. When user adds someone to their friends list, thats when the field will appear in mongodb. Right now I'm getting this error:
"message": "'$set' is empty. You must specify a field like so: {$set: {<field>: ...}}",
I'm not sure if I'm doing the $set correctly.
The UserType
const UserType = new GraphQLObjectType({
name: "User",
fields: () => ({
_id: { type: GraphQLString },
firebaseUid: { type: GraphQLString },
following: { type: new GraphQLList(GraphQLString) },
followers: { type: new GraphQLList(GraphQLString) },
...
})
});
edit:
current mongodb data collection:
_id: ObjectId("5e5c24111c9d4400006d0001")
name: "Mr. Smith"
username: "mrsmith"
after running the update
_id: ObjectId("5e5c24111c9d4400006d0001")
name: "Mr. Smith"
username: "mrsmith"
following: ["fdsaduybfeaf323dfa"] // <-- this gets added
Currently mongooses validator is rejecting the update. To fix this you need the following:
You only need to $push since it will automatically create an array if the property does not exist
You should remove the extra { } around the firebaseUid in the $push because otherwise the following array will contain objects with a firebaseUid property instead of directly containing the Uid (or would if the schema validator allowed it)
Mongo ObjectIds can only be converted from strings when they are 12-byte hexadecimal, and firebaseUid is not, so the schema should be typed to String instead of ObjectId as the validator will reject the field for update otherwise.

MongoDB validation allowing everything

I'm starting to use MongoDB node driver and I can't get validation to work at all.
I created the following validation objects using query:
validator: { $in: [
{ name: { $type: "string" } }
]}
And this JSON Schema:
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name"],
properties: {
name: {
bsonType: "string",
description: "is required and must be a string"
}
}
}
}
Then if I try to insert the following document with this structure {name: 2}, it gets added no failed validation whatsoever.
I've read the mongo and the node driver docs up and down regarding document validation and can't find a way to get this validated. I'm currently using Mongo version 3.6.7 and the node driver version 3.1.4, on an express server version 4.16.3.
This is the whole code:
// create a single user
const createSingleUser = (client, db) => {
db.collection("users").insertOne({
name: 2
}, (err, response) => {
if (err) console.warn(err);
console.log("new user added!!!!");
client.close();
}); // insert one
};
// create collection and add validator
const createUserCollection = client => {
const MongoDriverData = client.db("MongoDriverData");
// create the collection and add validation
MongoDriverData.createCollection( "users", {
validator: {
$jsonSchema: {
bsonType: "object",
required: ["name"],
properties: {
name: {
bsonType: "string",
description: "is required and must be a string"
}
}
}
}, // validator
validationAction: "error"
}, (err, results) => {
console.log( "Collection created!!!" );
// now insert a user
createSingleUser(client, MongoDriverData);
});
};

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.