If I use mongoose model to access MongoDB database such as
MyModel.findByID(...)
MyModel.save(...)
MyModel.aggregate(...)
Are the methods name and query syntax the same as if I am doing with MongoDB shell?
db.mycollection.findByID(...)
db.mycollection.save(...)
db.mycollection.aggregate(...)
In other words, does mongoose model inherits every collection method from MongoDB as listed here in https://docs.mongodb.org/manual/reference/method/#collection ? And does mongoose have built-in wrapper function around the MongoDB collection methods with different syntax?
Just wondering if I call MyModel.method() I can use the same syntax as I would use with MongDB shell.
Related
group_concat of sql in mongoose?
[Sequelize.fn('group_concat', Sequelize.col('id')), 'products']
Query written in sql using sequelize. I am expecting to convert it in to mongoose.
Use aggregation frameworks of mongodb
Conversion of above code is
id is equal to _id in mongodb
Insert below line of code in aggregation object.
{$group:{id:"_id",products:{Spush:"$_id"}}
What should be the query for delete field from mongo db collection on groovy language
I am looking to delete parameter from collection.
normal mongo db query its working
db.users.updateOne({"userId.name":"LastName"},{$unset:{nationality:"",occupation:"",friendlyName: ""}});
but using groovy it doesn't
collection.deleteMany(eq("userId.name", "Lastname"),combine(set("nationality", ""),set("occupation", ""),set("friendlyName", "")));
with this query it doesnt work.
You don't need this combine bit as for the user removal you don't need to unset the nationality, occupation and friendlyName
Consider using MongoCollection.deleteOne() function and provide only userId.name attribute like:
def deleteResult = collection.deleteOne(eq("userId.name", "LastName"))
More information: MongoDB Performance Testing with JMeter
I have collection with field price that is of the DataType Decimal. When I use insertMany, how to set it to store in decimal type ?
let data=[{id:'1', price:10}, {id:'2', price:20}]
insertMany(data)
I user meteor and mongo node driver.
To insert multiple document in MongoDB collection you can use the insert method of MongoDB driver for nodejs. Meteor provides rawCollection method to access to the the Collection object corresponding to the collection from the MongoDB driver module which is wrapped by Mongo.Collection.
In your case the query will be like this:
let data=[{id:'1', price:10}, {id:'2', price:20}]
CollectionName.rawCollection().insert(data);
I am using the mongo-db java driver 3.8 and work with the collection as follows:
MongoDatabase md=mongoClient.getDatabase(databaseName);
MongoCollection<ConstructionPlan> collection=md.getCollection(plansCollectionName,abc.class);
collection.insertOne(item);
collection.find(Filters.eq("itemId", id),abc.class).first();
With this code I do not have to do any conversion. I was looking for a way to update an document the same way. I am thinking on something like this:
abc anABCObject=collection.find(Filters.eq("itemId", id),ConstructionPlan.class).first();
//updates...
collection.update(anABCObject);
Is there a way to update an existing document without BSON conversion? (I was unable to find it....)
updateOne for updating document fields using update operators.
You need replaceOne which takes the replacement document.
collection.replaceOne(
Filters.eq("itemId", id),
anABCObject
);
I'm using DoctrineMongoDBOBundle with Symfony2.
I've a Document Product which has an annotation referenceOne to other Document Price.
I want to sort by price when I fetch with queryBuilder.
$qb = $dm->createQueryBuilder('MyBundle:Product')
->field('geocoordinates')
->near('lat','lon')
->sort('hasPrice','desc')
But this doesn't works. Perhaps for the use of near?
It depends of toString() method of Document Price?
Regards.
I've a Document Product which has an annotation referenceOne to other Document Price.
There are no joins in MongoDB and I do not believe Doctrine does client side aggregation and sorting here. As such this wouldn't work anyway.
However sorting will work on a $near command ( http://docs.mongodb.org/manual/reference/operator/near/ ) which is what Doctrine should be using in this case, here you can see explicit support for $near being identified through the command you are using: https://github.com/doctrine/mongodb/commit/59f73cde2c15d171ff39afbf46c1a1339a51048c so your problem is the linked document, MongoDB has no JOINs.
In this case, hasPrice looks like it corresponds to a method, that perhaps checks whether the price reference is null or not. When referring to fields in ODM queries, names of class properties may be translated to the MongoDB field names (if they differ), but there is no support for method evaluation. Sammaye was correct that Doctrine does no client-side aggregation or sorting.
As an alternative, you may be able to sort on the price.$id field, which should group nonexistent values together in the results. Similarly, you could use the $exists operator to match/exclude based on whether a document was actual referenced. References in ODM (excluding the "Simple" type) are stored as MongoDBRef instances, which translate to objects with $id, $ref, and $db fields. As a result, you can query on those values just like any other field in your document.