mongodb

How to remove a document inside an array in mongodb using $pull

限于喜欢 提交于 2021-01-29 05:29:45
问题 I have the following document structure { "_id" : ObjectId("5ffef283f1f06ff8524aa2c2"), "applicationName" : "TestApp", "pName" : "", "environments" : [], "stages" : [], "createdAt" : ISODate("2021-01-15T09:51:35.546Z"), "workflows" : [ [ { "pName" : "Test1", "wName" : "TestApp_Test1", "agent" : "" }, { "pName" : "Test2", "wName" : "TestApp_Test2", "agent" : "" } ], [ { "pName" : "Test1", "wName" : "TestApp_Test1", "agent" : "" } ] ], "updatedAt" : Date(-62135596800000) } I wish to remove the

GO Mongodb时间处理

回眸只為那壹抹淺笑 提交于 2021-01-29 05:28:56
go在操作monogdb时间的时候如果使用time.Time时间格式,则前后端的时间显示是这样的:2021-01-28T06:38:43.622Z;这种格式很不友好(时区还不对),更多时候我们可能需要的是这种格式:2021-01-28 14:38:43;在网上查了一些资料,发现解决方案都不太好,有的是要手动做转换,有的是改变了mongodb的存储内容;我这里综合了一些资料给出以下解决方法(通过申明time.Time的扩展,重写json和bson序列化代码实现): 序列化处理代码: package common import ( "errors" "fmt" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" "go.mongodb.org/mongo-driver/x/bsonx/bsoncore" "time" ) type JsonTime time.Time const ( timeFormart = "2006-01-02 15:04:05" ) //实现json反序列化,从传递的字符串中解析成时间对象 func (t *JsonTime) UnmarshalJSON(data [

update nested object in array Mongoose

两盒软妹~` 提交于 2021-01-29 05:20:27
问题 I would like the nested object with the id 5f0e14241f5ccb42d8742767 to be updated: { "_id": "5f0b33ab52a4e966c754d963", "make": "Toyota", "model": "Prius", "engine": "1.8", "insurances": [ { "yearlyPremium": { "$numberDecimal": "60.39" }, "_id": "5f0e14241f5ccb42d8742767", "startDate": "1970-01-19T09:31:15.550Z", "monthlyPremium": { "$numberDecimal": "5.49" }, "excess": 100 }, { "yearlyPremium": { "$numberDecimal": "71.39" }, "_id": "5f0e147c0340243eb03b5247", "startDate": "1970-01-19T09:31

How to use fetchNewObject with update.one ReactiveMongo?

不想你离开。 提交于 2021-01-29 05:14:26
问题 Previously I used findAndUpdate and could add fetchNewObject = true so I was able to do something like this after the query: .map(_.result[WhicherReport].getOrElse { throw new NoSuchElementException }) but I'm using transaction now and could only perform update.one(...) and there is no option to pass it fetchNewObject , what can I do? This is my func: def someUpdateFunc(collection: BSONCollection, metadata: Metadata, ids: List[String]): Future[UpdateWriteResult] = { collection.update.one( q =

missing type in composite literal go AND missing key in map literal go

橙三吉。 提交于 2021-01-29 05:13:44
问题 Im trying to do Pagination with MongoDB I write this code: findOptions := options.Find() findOptions.SetLimit(20) findOptions.SetSort(bson.M{{"_id", 1}}) cursor, err34 := collection.Find(context.Background(), bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions) Now It keeps complaining: missing type in composite literal go AND missing key in map literal go It complains for this part: findOptions.SetSort(bson.M{{"_id", 1}}) and bson.M{{"_id", bson.M{{"$gte", last_id}}}}, findOptions) I'm

Is it possible to see the incoming queries in mongodb to debug/trace issues?

别来无恙 提交于 2021-01-29 05:09:59
问题 I have mongo running on my macbook (OSX). Is it possible to run some kind of a 'monitor' that will display any income requests to my mongodb? I need to trace if I have the correct query formatting from my application. 回答1: You will find these tools (or utilities) useful for monitoring as well as diagnosing purposes. All the tools except mtools are packaged with MongoDB server (sometimes they are installed separately). 1. Database Profiler The profiler stores every CRUD operation coming into

How to get set of missing consecutive numbers array only has low and high mongodb

无人久伴 提交于 2021-01-29 05:04:29
问题 I am trying to get sets of having an array of missing consecutive numbers only has [low, high] Example, mongodb collection 'meta_import' has these numbers: [1,2,3,4,7,8,100,101,200,400,401,403] I want to get [[5,6], [9,99], [102,199], [201,399], [402]] I have tried: var pipe = [ {'$group' : {'_id' : null, min : {'$min' : "$num"}, max : {'$max' : "$num"}}}, {'$addFields' : {'rangeIds' : {'$range' : ["$min", "$max"]}}}, {'$lookup' : {'from' : "meta_import", localField : "rangeIds", foreignField

Spring + MongoDB - MongoTemplate + Criteria Query

这一生的挚爱 提交于 2021-01-29 05:01:32
问题 I am using Spring Boot + MongoDB. I need to query database based on some criteria where my methods looks like below: @Override public List<MyCollection> findBuyByCriteria(Request request) { Query search = new Query(); search.addCriteria(Criteria.where("ItmId").in(request.getItmIds())); return mongoTemplate.find(search, MyCollection.class); } Problem that I am facing is: At line search.addCriteria(Criteria.where("ItmId").in(request.getItmIds())); request.getItmIds has 1 million Ids due to

Inserting an Image to MongoDB through Node.js

爷,独闯天下 提交于 2021-01-29 04:21:09
问题 I am having trouble inserting an image to MongoDB through node.js. My code is as follows: var express = require('express'), mongoose = require('mongoose'), fs = require('fs'), Schema = mongoose.Schema; app = express(), port = process.env.PORT || 3000; var imagePath = '~\images\image1.png'; mongoose.connect('mongodb://localhost/test'); var db = mongoose.connection; var contentSchema = new Schema({ _id: Schema.ObjectId, date: {type: Date, default: Date.now}, user_ip: Number, likes: Number,

geoNear returns incorrect distance

前提是你 提交于 2021-01-29 04:09:59
问题 I'm playing with geo features of mongodb in Java and i'm facing to the following problem running command 'geoNear': Given: A collection named 'GEOENTITIES' with GeoJSON objects This collection is indexed with the Java code (using Jongo): collection.ensureIndex("{ coordinates : '2dsphere' }"); A document (named 'A' for the example) exists with coordinates equals to [48.0, 9.0] When executing : {geoNear: 'GEOENTITIES', near: [48.0,9.1], spherical: true, num: 5, distanceMultiplier: 6371} Then: I