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"}}
Related
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 write the criteria query like this
query.addCriteria(Criteria.where("_id").is(id));
but the result is {"_id":123456789123450} which is not searching result. What I need to change to search the result using the criteria query on _id?
There are two ways to do this
Query query = new Query(Criteria.where("id").is(new ObjectId(<id value>)));
Or Spring data MongoDB's findById
org.springframework.data.repository.CrudRepository.findById(String id);
https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/repository/CrudRepository.html#findById-ID-
I've written an operation upsert a document to Mongo using Morphia.
I have a field which I want to save as an int32, but after the upsert it is inserted as int64.
I have made sure that I convert the long as an int using Long.intValue() and the object that Morphia serializes back to, the member field is int. I have also checked the UpdateOperations.ops to see what morphia is upserting.
The upsert operation is:
UpdateOperations<Test> ops = datastore.createUpdateOperations(Test.class)
.set("test_field", testField.intValue())
The current version of Mongo I am using is 3.0.
Any help would be appreciated!
Thanks!
EDIT:
Looking at the update query operation in Morphia it is:
{$set={test_field=11}}
I managed to find out the solution. The query for the upsert was querying on the test_field as a long rather than an int. Mongo 3.0 sees this as the type to insert - however running on Mongo 3.2, there is no issue and it will upsert with the type specified in the upsert operation, not the query.
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.
Converting the MongoDb queries into postgresql.
In mongoDb we the basic usage of $push is as follows :
https://docs.mongodb.org/v3.0/reference/operator/aggregation/push/
Is there any alternative for this in postgresql or we need to implement it using the code.
You can do this using array_agg function in Postgres.
See Sample code(shamelessly copied ) : http://www.sqlfiddle.com/#!15/21b2b/1