We have migrate our database to MongoLab via Heroku but now our cloudcode doesn't work anymore.
After looking the code, it seems all the features of Geopoint doesn't work with MongoDB.
Here is our code:
var userGeoPoint = new Parse.GeoPoint(latitude, longitude);
var query = new Parse.Query("User");
query.near("location", userGeoPoint);
query.limit(10);
query.find({
success: function(placesObjects) {
console.log("yes");
},
error:function(status) {
console.log(status);
}
});
And here is the answer:
Result: {"code":1,"message":"internal error"}
Any help will save my life...
Thanks!
I ran into the same issue originally but I fixed it with set failIndexKeyTooLong=false. Sandbox from mLab doesn't support this feature.
Related
so I'm currently trying to develop a mmorpg from scratch and got up to trying to save users from my client side (I'm using game maker studio). Im using robomongo as my Mongodb management tool.
My client connects just fine to my server and im able to take in a username and password but nothing is being saved to the database i created. Here is my user.js file:
var mongoose=require('mongoose');
var userSchema=new mongoose.Schema({
username: {type: String, unique:true},
password: String,
sprite:String,
current_room: String,
pos_x:Number,
pos_y:Number
});
userSchema.statics.register=function(username,password,cb){
var new_user=new User({
username: username,
password: password,
sprite: "spr_Hero",
current_room:maps[config.starting_zone].room,
pos_x:maps[config.starting_zone].start_x,
pos_y:maps[config.starting_zone].start_y,
});
//save user to database
new_user.save(function(err){
if(!err){
cb(true)
}else{
cb(false);
}
});
};
userSchema.statics.login=function(username,password,cb){
//findOne searched db - mongodb specific method
//searches db using json objects (cant do that in other sql/mysql)
User.findOne({username:username},function(err,user){
if(!err && user){
if(user.password==password){
cb(true,user);
}else{
cb(false,null);
}
}else{
//error||user doesnt exist
cb(false,null);
}
})
};
//export into gloabl space of our application
module.exports=User=gamedb.model('User',userSchema);
and here is my mongodb.js file. I configured the database location in my config.js. Im just using my local ip. Any help on fixing this would be great thanks!
var mongoose=require('mongoose');
mongoose.Promise=global.Promise;
module.exports=gamedb=mongoose.createConnection(config.database);
Seems like you are using MongoDB 3.4 which is not supported by Robomongo currently, but it will be soon. You can follow this ticket for updates: https://github.com/Studio3T/robomongo/issues/1250
Note: I am one of the contributors of Robomongo.
MongoInternals.RemoteCollectionDriver("mongodb://#{server.ip}:#{server.port}/#{server.dbName}")
If I call multiple remote MongoDB methods and if there are collecitons with the same names, Meteor throws the error something like this, "collectionName/insert is already exist..."
I think Meteor creates each collection's methods internally so that control each collection, but I need to control several MongoDB at a time with some reasons.
How can I avoid this situation?
In addition,
I realize I can use Npm Mongo driver directly like this without any NPM package involving.
var MongoClient = MongoInternals.NpmModules.mongodb.module.MongoClient;
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
try {
var collection = db.collection('documents');
collection.find({}).toArray(function(err, docs){
console.log(err);
console.log(docs);
});
}
catch(err) {
console.log(err);
}
db.close();
});
But this still forces me to control each DB with the Node.js callback style.
Is there any idea to avoid this?
I have been checking this issue, and I found a way to do it.
The solutions that I had seen to connect several databases where:
storageServerDriver = new MongoInternals.RemoteCollectionDriver("mongodb://ip:port/dbName")
#Collection = new Mongo.Collection("collection", { _driver: storageServerDriver })
But as you mentioned before, with two collections with same name, an error was thrown (internally Meteor identifies the collections by their name, so it tries to override the structure of the collection already created).
Anyway, to fix this, you can use the following hack:
storageServerDriver = new MongoInternals.RemoteCollectionDriver("mongodb://ip:port/dbName")
#CollectionTwo = storageServerDriver.open('collection')
I have mongoosastic setup within a MEAN stack program. Everything works correctly except when I delete a document from mongodb it is not deleted in the elasticsearch index. So every time I do a search that includes delete items, the deleted item is returned but is null when it is hydrated. Does mongoosastic handle deleting from the ES index? Do I have to program an index refresh?
var mongoose = require('mongoose');
var mongoosastic = require("mongoosastic");
var Schema = mongoose.Schema;
var quantumSchema = new mongoose.Schema({
note: {
type: String,
require: true,
es_indexed: true
}
});
quantumSchema.plugin(mongoosastic);
var Quantum = mongoose.model('Quantum', quantumSchema);
Quantum.createMapping(function(err, mapping){
if(err){
console.log('error creating mapping (you can safely ignore this)');
console.log(err);
}else{
console.log('mapping created!');
console.log(mapping);
}
});
I had the same error. If you look in the Documentation it states that you have to explicit remove the document after deleting it.
This is the way i am doing a deletion now.
const deleteOne = Model => async (id)=> {
const document = await Model.findByIdAndDelete(id);
if (!document) {
return new Result()
.setSuccess(false)
.setError('Unable to delete Entity with ID: ' + id + '.')
}
//this ensures the deletion from the elasticsearch index
document.remove();
return new Result()
.setSuccess(true)
.setData(document)
}
I dont know what version of mongoosastic you're using but i use mongoosastic#3.6.0 and my indexed doc get deleted whenever i remove it either using Model.findByIdAndRemove or Model.remove. Therefore try to cross check the way you delete you're docs.
I solved the problem by changing the way I delete the data.
I was using:
Quantum.findByIdAndRemove(quantumid)
I switched it to:
Quantum.findById(quantumid, function(err, quantum) {
quantum.remove(function(err, quantum) {
if (err) {
console.log(err);
return;
}
});
});
I did not research the reason for this working, but it solved the problem and I moved on.
I am new to mongodb and node.js. I have a simple node.js app that in order to run I have to run the command
mongod
to make sure mongodb is running.
locally this works fine.
now I am moving to heroku, I createrd a Procfile where I declare :
web: node http.js
so this take care of starting node.js.
Now how can I call the command
mongod
on heroku?
Update
before the heroku requirement this is how I used to initialize my mongodb :
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure,
ObjectID = mongo.ObjectID;
var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sampledb', server);
db.open(function(err, db) {
if(!err) {
console.log("Connected to 'sampledb' database");
db.collection('sample', {strict:true}, function(err, collection) {
if (err) {
console.log("The 'sample' collection doesn't exist. Creating it with sample data...");
populateDB();
}
});
}
});
in order to accomodate heroku here is how I initialize mongodb :
var mongo = require('mongodb');
var Server = mongo.Server,
Db = mongo.Db,
BSON = mongo.BSONPure,
ObjectID = mongo.ObjectID;
var mongoUri = process.env.MONGOLAB_URI ||
process.env.MONGOHQ_URL ||
'mongodb://localhost/sampledb';
mongo.Db.connect(mongoUri, function (err, db) {
db.collection('sample', function(er, collection) {
if (err) {
console.log("The 'sample' collection doesn't exist. Creating it with
sample data...");
populateDB();
}
});
});
Now this throws an error because db is not recognized. I think I am missing a conceptual issue here, I am new to this world all together.
any help is appreciated.
You don't call the command mongod on heroku.
You'd add one of the mongo add-ons from addons.heroku.com to your app, then have your code connect to the credentials in the config variable exported to your app by the add-on provider.
I'm trying to remove all items in a collection.
db.collection('sessions', function(err, collection) {
collection.remove();
});
This is the error I get:
node.js:134
throw e; // process.nextTick error, or 'error' event on first tick
^
TypeError: Cannot call method 'getRequestId' of null
at [object Object].executeCommand (/srv/www/www.cidev.com/nodejs/node_modules/mongodb/lib/mongodb/db.js:778:48)
at Collection.remove (/srv/www/www.cidev.com/nodejs/node_modules/mongodb/lib/mongodb/collection.js:199:26)
at /srv/www/www.cidev.com/nodejs/session/memory/index.js:15:20
at [object Object].collection (/srv/www/www.cidev.com/nodejs/node_modules/mongodb/lib/mongodb/db.js:197:12)
at new <anonymous> (/srv/www/www.cidev.com/nodejs/session/memory/index.js:14:11)
at Object.<anonymous> (/srv/www/www.cidev.com/nodejs/session/memory/index.js:157:16)
at Module._compile (module.js:411:26)
at Object..js (module.js:417:10)
at Module.load (module.js:343:31)
at Function._load (module.js:302:12)
However, I can do this via mongodb fine:
db.sessions.remove();
What's the best way to achieve what I want via node?
Thanks
Going back to this... just to update the question.
store.collection('sessions',function(err, collection){
collection.remove({},function(err, removed){
});
});
I know this is a little late to the party and much has changed, but to remove a collection in node, you do this:
db.collection('someCollection').drop();
From the mongo terminal you do this:
db.someCollection.drop();
It's that simple.
Providing a more recent answer, based on API changes and trying the previous answers myself.
The following example represents a block of code as we use it in our integration tests:
const mongoUrl = 'mongodb://localhost:27017/test-database';
const mongoClient = await MongoClient.connect(mongoUrl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
const database = mongoClient.db();
if (database) {
const collections = await database.listCollections();
await collections.forEach(async collection => {
console.log(`Dropping ${collection.name}`);
await database.collection(collection.name).drop();
});
}
Note, while you could use the following, it will fail if you don't have full permissions over the database, such as in Mongo Cloud:
await database.dropDatabase();
For context, I was using mongodb package 3.5.9 and node 14.15.1.
I'm not sure whether you were able to figure this out, but your code did not work for me...perhaps because of changes in the API? Anyway I was able to remove an entire collection's contents using the following code:
db.CollectionName.remove().exec(function(error) {
if(error) {
console.log('Uh oh: ' + error);
}
else {
console.log(' [Existing Collection Deleted]');
}
});