This question already has answers here:
Node.js - Mongoose - Check if a collection exists
(4 answers)
Closed 8 years ago.
I am new to mongo Db and need to see if schema is already been created or not. I am using mongoose and node.js. If its not created i need to somehow run the creation script for once or else proceed with other stuff.
TIA
Assuming that by schema you mean that you want a check if the collection is already created in mongoDB, you should check this question here that explains the solution.
Quoting it:
Assuming you have a Mongoose Connection object named conn that's been opened using mongoose.createConnection, you can access the native mongo Db object via conn.db. From there you can call collectionNames which should provide what you're looking for:
conn.db.collectionNames(function (err, names) {
// names contains an array of objects that contain the collection names
});
You can also pass a collection name as a parameter to
collectionNames to filter the results to just what you're looking for.
Related
This question already has answers here:
Mongoose: findOneAndUpdate doesn't return updated document
(16 answers)
Closed 3 years ago.
I am working on node js app using express.js and mongodb with mongoose module to handle the database.
in one case I need to update a user property languages using findOneAndUpdate method. this property is an array of objects should looks like [{"language":"Ar","level":4}]
when I update it using the following nodejs code :
User.findOneAndUpdate({_id:mongoose.Types.ObjectId(req.body.id)},
{$set:{[req.body.key]:req.body[req.body.key]}},(err,doc)=>{
console.log(req.body) // {id:5d1619fa7c11fa102210ef86,"languages":[{"language":"Ar","level":4}],key:"languages"}
if (!err) {
res.json(200,doc);
}else{
res.json(500,{error:err});
}
})
I get the following results
but when I try same thing from the mongo shell
db.users.findOneAndUpdate({"_id" : ObjectId("5d1619fa7c11fa102210ef86")},{ '$set': {"languages":[{"language":"Ar","level":4}]} })
I get the correct result
which is the correct expected results.
any idea why the nodejs snippet not working properly.
thanks in advance.
I think you are not getting the updated result back after update has successfully run. you need to pass new : true in your update query as the third argument.
By default mongodb returns the old document after the update, but you can specify to return the updated document in the query.
Try this :
User.findOneAndUpdate({_id:mongoose.Types.ObjectId(req.body.id)},
{$set:{[req.body.key]:req.body[req.body.key]},{new:true},(err,doc)=>{
...
//doc will be updated doc here
})
This question already has answers here:
How to store large numbers in MongoDB with Node.js
(2 answers)
Closed 4 years ago.
Now I use mongoose to define a schema like below:
const schema = new Schema({
roleId:Number,
type:Number,
_id:false
});
What I actually want to fulfill is to create a document whose field roleId's type is NumberLong and field type's type is NumberInt. However, the new document's fields created with this schema are all with
type of NumberInt.
After reading the mongoose's docs, I still have no idea to define schema with different number types.
So, if there is any solution to solve my problem?
SchemaType stores a number value, with restrictions. Mongoose does not natively support long and double datatypes for example, although MongoDB does. However, Mongoose can be extended using plugins to support these other types.
You can use "mongoose-long" npm for the purpose.
This question already has an answer here:
What collection does Mongoose put things in?
(1 answer)
Closed 7 years ago.
I'm using mongoose and i have a schema called MessageSchema which i use through a MessageModel to store messages in a database.
This is already working as i can save instances and retrieve them using MessageModel.find(...)
I'm running mongodb and nodejs on Windows. So, when i start the mongo shell by executing "mongo" in a cmd window and try to do:
use mydb
db.mydb.find()
nothing comes up.
How can i see these entries?
Your current command you are switching to your database but then you are trying to query a collection called mydb in your mydb database. When you type use mydb you are making the db object point to your database. Now you have access to the collections in that databse using
db.<collection>.find()
The correct command you want to use would be
use mydb;
db.MessageModel.find();
This question already has answers here:
Mongoose update/upsert?
(5 answers)
Closed 7 years ago.
What is the best method of updating a row in a Mongo DB from NodeJS (preferably with Mongoose) while row if it does not already exist, create it. I've tried multiple functions, save findOneAndUpdate update. They are all dependant on one another, the row has to exist to be able to update it, for instance.
How can we combine these features into a function:
If the entry does not exist by 'name', create it, and then...
If the entry does exist, update 'password' with 'data'
Assuming a mongo collection that could look like:
{
"_id": ObjectId("5357c8e0b12b6c375bb0217b"),
"name": "Kimberly",
"password": "data"
}
My current code is a mess with a lot of if statements and relying on err responses to call database functions. (Check if a user exists before adding them, for example) Is there a better way to make sure there are no duplicate entries in the database after updating or saving something?
Many thanks
you may use findAndModify with upsert=true
http://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Name already used by an existing object in VBA
I am using Oradynaset object to fetch the results from data base in VBA but the same object i.e OraDynaset does not work to create new table as i use the following query
strsql="create table abs.test as (select * from emp)"
Oradynaset=objDatabase.DBCreatedynaset(strsql,o&)
after running this query i get the error - ORA 009955:Object is used by an existing object.
can you please help me on this.
If you want to create/alter/drop databases and tables(ie. DDL), you should use the ExecuteSQL function of Objectdatabase object.
Oradynaset function of obdatabase objectis used for executing DML command in case of which it stores the fetched result that can be used later by looping through it.
Try by using object database object with sql query as parameter