mongodb getting TypeError: newUser.save is not a function

Published on

You have got some things wrong. Something like this (User refers to your schema): var user = new User(); user.name = req.body.name; user.email = req.body.email; user.password = req.body.password; user.info = req.body.user_bio; user.save().then(function(err, result) { console.log(‘User Created’);}); should work better. Instead of passing a new object (which obviously doesn’t contain the save method), you are now … Continue reading mongodb getting TypeError: newUser.save is not a function

How to convert mongo document into key value pair in .Net?

Published on

There is a ToDictionary method that can map this. var document = collection.Find(filter).Project(projection); var dictionary = documents.ToEnumerable().ToDictionary(key => key.client, value => value.url); A different approach var docs = new Dictionary<string, BsonDocument>(); var result = collection.Find(filter).Project(projection); result.ForEachAsync((bsonDoc) => { string name = bsonDoc.GetValue(“[Your Dictionary Key]”).AsString; if (!docs.ContainsKey(name)) { docs[name] = bsonDoc;}});

How to delete the last item of a collection in mongodb

Published on

Be it first or last item, MongoDB maintains unique _id key for each record and thus you can just pass that id field in your delete query either using deleteOne() or deleteMany(). Since only one record to delete you need to use deleteOne() like db.collection_name.deleteOne({“_id”: “1234”}) // replace 1234 with actual id

How to find particular data using a filter in mongodb using node js

Published on

Below is the example, How to find particular data using a filter in mongodb using node js? 1).This is my schema where I store question id, score and time for every single question child:{ quiz: questionId:{type:String}, score:{type:Number}, time:{type:String}}} 2). This is question schema _id:{type:String}, question:{type:String}, answer:{type:String} 3). This is my node js code use to fetch … Continue reading How to find particular data using a filter in mongodb using node js

How to store and view or retrieve Images in MongoDB using PHP

Published on

Here’s an example of inserting/getting back an image: $m = new Mongo(); $db = $m->example; $gridFS = $db->getGridFS(); $id = 123; // store $gridFS->storeFile(“someFile.jpg”, array(“_id” => $id)); // retrieve echo $gridFS->findOne(array(“_id” => $id))->getBytes(); Try querying $db->fs->files to see what _ids you should be querying for. Also, you can’t create a MongoId from an arbitrary string. … Continue reading How to store and view or retrieve Images in MongoDB using PHP