mongodb

MongoDB count by referenced document property

让人想犯罪 __ 提交于 2021-02-19 08:26:22
问题 db.foos { bar: ObjectId('123') } db.bars { _id: ObjectId('123') type: 'wine' } How can I in the simplest way find the number of foo-documents that refers to a bar-document of type 'wine'? Hopefully one that scales to perform fairly well even if the collections should contain a very large number of documents. 回答1: Try this aggregation framework query: db.foos.aggregate([ {$lookup: { from: "bars", localField: "_id", foreignField: "_id", as: "docs" } }, {$unwind: "$docs"}, {$match: {"docs.type":

How to insert new object without deleting previous one

坚强是说给别人听的谎言 提交于 2021-02-19 07:44:09
问题 I'm setting up my new RocketChat Server. The problem is that actually, Rocket Chat doesn't support CAS Account if they are not created by CAS. We have old accounts. I can add the CAS feature by doing this : Enter in the MongoDB Database with MongoDB Compass software on Windows, and add cas object in services object with my keyboard... https://i.imgur.com/AIMAlA6.png So as you can see I can add the CAS feature by doing this. I want to do that with code so I did this : 1 - Enter in rocketchat

How to select between dates Spring data MongoDB using @Query

人盡茶涼 提交于 2021-02-19 06:53:05
问题 I'm using Spring data with MongoDB and i need to find between actual day and 7 days ahead. I have create repsoitories with @Query annotation and don't like to user Criteria class. Do you have some idea how to user between with @Query? thanks in advance. 回答1: You can try below query. Using @Query annotation @Query(value = "{'date':{ $lt: ?0, $gt: ?1}}") List<SomeClass> findByDateBetween(Instant from, Instant to); Or Using repository supported keywords List<SomeClass> findByDateBetween(Instant

How to select between dates Spring data MongoDB using @Query

青春壹個敷衍的年華 提交于 2021-02-19 06:52:09
问题 I'm using Spring data with MongoDB and i need to find between actual day and 7 days ahead. I have create repsoitories with @Query annotation and don't like to user Criteria class. Do you have some idea how to user between with @Query? thanks in advance. 回答1: You can try below query. Using @Query annotation @Query(value = "{'date':{ $lt: ?0, $gt: ?1}}") List<SomeClass> findByDateBetween(Instant from, Instant to); Or Using repository supported keywords List<SomeClass> findByDateBetween(Instant

How to select between dates Spring data MongoDB using @Query

纵饮孤独 提交于 2021-02-19 06:52:09
问题 I'm using Spring data with MongoDB and i need to find between actual day and 7 days ahead. I have create repsoitories with @Query annotation and don't like to user Criteria class. Do you have some idea how to user between with @Query? thanks in advance. 回答1: You can try below query. Using @Query annotation @Query(value = "{'date':{ $lt: ?0, $gt: ?1}}") List<SomeClass> findByDateBetween(Instant from, Instant to); Or Using repository supported keywords List<SomeClass> findByDateBetween(Instant

How can I find similar documents in MongoDB?

可紊 提交于 2021-02-19 06:19:07
问题 I have food db listing similar to: { Name: "burger", ingredients: [ {Item:"bread"}, {Item:"cheese"}, {Item:"tomato"} ] } How can I find documents that have the most similar items in ingredients ? 回答1: First of all, your data should be remodelled as below: { name: "Burger", ingredients: [ "bread", "cheese", "tomato", "beef" ] } The extra "Item" does not add any additional information nor does it help accessing the data in any way. Next, you need to create a text index. The docs state that text

Add optional query parameter using spring data mongodb repository

試著忘記壹切 提交于 2021-02-19 06:07:20
问题 I want to add optional query parameters using spring data mongodb. Controller code: @RestController private final ActionService actionService; @RequestMapping(value = "/action/{id}", method = RequestMethod.GET) public ResponseEntity<List<Action>> getActionList(@PathVariable("id") long id, @RequestParam(value = "actionType", required = false) ActionType actionType, @RequestParam(value = " ", required = false) String[] params) { List<Action> actionList = actionService.getAction(id, actionType,

failed with Received heartbeat from member with the same member ID as ourself:0

可紊 提交于 2021-02-19 05:32:40
问题 Trying to set a replica set configuration for 1 primary , 1 slave and 1 arbitrator. I have setup this in the /etc/mongodb.conf replication: replSetName: ProductionReplicaSet but accidentally ran rs.initiate() on the three servers. now when I'm running rs.add("mongo02....") i'm getting : { "ok" : 0, "errmsg" : "Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: chef-production2-mongo01:27017; the following nodes did not

failed with Received heartbeat from member with the same member ID as ourself:0

旧时模样 提交于 2021-02-19 05:31:36
问题 Trying to set a replica set configuration for 1 primary , 1 slave and 1 arbitrator. I have setup this in the /etc/mongodb.conf replication: replSetName: ProductionReplicaSet but accidentally ran rs.initiate() on the three servers. now when I'm running rs.add("mongo02....") i'm getting : { "ok" : 0, "errmsg" : "Quorum check failed because not enough voting nodes responded; required 2 but only the following 1 voting nodes responded: chef-production2-mongo01:27017; the following nodes did not

MongoDB Can't canonicalize query: BadValue Too many text expressions

坚强是说给别人听的谎言 提交于 2021-02-19 05:20:26
问题 I am attempting to build a query for MongoDB in Java and I am running into this error, "Can't canonicalize query: BadValue Too many text expressions" whenever I run the query. The database is full of documents with a text property that is indexed for full text searching. I am trying to build a query to find any documents that match one or more of the queries in queryList and are within a certain time range. It works fine if there is only one query in the queryList, but fails otherwise. Any