Menu
  • HOME
  • TAGS

storing a matrix as a collection in MongoDB

Tag: mongodb,matrix,distance,point,asymmetric

I have a bunch of points {A, B, C, ...., X} and I want to store their distances in a matrix. One extra complication is that the distance from A to B and not the same as the distance from B to A, they are asymmetric.

My aim is to store this matrix in a collection in MongoDB but I really don't know how to, is that possible? any advice/ guidance is much appreciated.

Best How To :

What measure of "distance" are you using, out of curiosity? Technically, you can't call a function d(X, Y) a "distance" unless d(X, Y) = d(Y, X) always. Distance between points on a sphere is symmetric so you can't be using that metric. If all you want to do is store and retrieve values d(X, Y), just store documents like

{
    "from" : X,
    "to" : Y,
    "distance" : 691
}

where X and Y are whatever kind of values are appropriate. Put an index on { "from" : 1, "to" : 1 } and then define

function d(X, Y) {
    return db.distances.findOne({ "from" : X, "to" : Y }).distance
}

Query not working - MongoDB

mongodb

Looks like you've missed some brackets before $elemMatch: db.items.find( { "$or": [ {"f": {$elemMatch: {"t": "ry", "v": {$gt: 1980}}}}, {"f": {$elemMatch: {"t": "g", "v": {$in: ["Drama"]}}}} ] } ) ...

Is there a built-in function to get all unique values in an array field, across all records?

arrays,node.js,mongodb,mongoose,schema

You can use the distinct function to get the unique values across all category array fields of all documents: Article.distinct('category', function(err, categories) { // categories is an array of the unique category values }); Put an index on category for best performance....

How to add new items to an array in MongoDB

arrays,node.js,mongodb

$set is not an array update operation. The $set operator replaces the value of a field with the specified value. You just want to use $push by itself, as in .update({_id: id}, {$push: {name: item}}) You can't interpolate object property names in raw object declarations, so if you want to...

Interpolation inside a matrix. Matlab

matlab,matrix

M =[0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 2 0 0 0 0 0 1 0 1 0 0 0 1 0 4 0 0 0 0 0 3 0 0 6 0 0 4 0 0 3 0 0...

Use $or operator in @Query annotation in spring data mongodb repository

java,spring,mongodb,spring-data-mongodb

Per MongoDB reference for $or, your Query should be @Query("{'$or':[ {'type':?0}, {'name':?1} ]}") you'll need to give pass type and name params....

How do I apply multiple matrices in spriteBatch.Draw()?

c#,matrix,xna

Simply multiply the matrices before passing to spriteBatch.Begin. Matrix multiplication allows the effects of both matrices appended into a single matrix. Note that this operation is not commutative, meaning that A * B != B * A. In your case, you should probably multiply scaling matrix with the camera matrix....

Analytical solution for Linear Regression using Python vs. Julia

python,matrix,julia-lang

A more numerically robust approach in Python, without having to do the matrix algebra yourself is to use numpy.linalg.lstsq to do the regression: In [29]: np.linalg.lstsq(X, y) Out[29]: (array([[ 188.40031942], [ 0.3866255 ], [ -56.13824955], [ -92.9672536 ], [ -3.73781915]]), array([], dtype=float64), 4, array([ 3.08487554e+03, 1.88409728e+01, 1.37100414e+00, 1.97618336e-01])) (Compare the...

using MongoDB aggregate count subdata

mongodb,aggregate

please try the below query : db.collection.aggregate( [ { "$unwind": "$data.order_goods" }, { "$group" : { "_id" : "$data.order_goods.category", "total": {"$sum":1} } }, { "$project" : { "category" : "$_id" , total : 1 } ] ); ...

Use JSON file to insert data in database

javascript,json,mongodb,meteor,data

Simple use underscores _.extend function. Like this: var newProfile = _.extend( JSON.parse(Assets.getText('test.json')), {user: id} ) Profiles.insert(newProfile) ...

push item in sub document

mongodb,mongodb-query

You need the positional $ operator in your update portion of the statement: db.getCollection('forms').update( { "_id" : ObjectId("557e8c93a6df1a22041e0879"), "Questions._id" : ObjectId("557e8c9fa6df1a22041e087b") }, { "$push" : { "Questions.$.DataSource" : { "_id" : ObjectId("557e8e5ea6df1a27b403ff6b"), "CreationDate" : ISODate("2015-06-15T08:35:42.923Z"), "IsActive" : true, "Text" : "op", "Value" : "op" } } } ) It identifies...

Are the changes done by db command in MongoDB permanent and how?

mongodb,mongodb-query

Yes, Changes made to data in MongoDB shell are permanent but for configuration parameters, they will last only till next restart of instance if they are not specified in config file. MongoDB runs with default values if config file is not specified. You can specify config file as, mongod --config...

How to set first column to a constant value of an empty np.zeros numPy matrix?

python,numpy,matrix,modeling

All you have to do is to change head[0][0:] to head[:,0]=16 If you want to change the first row you can just do: head[0,:] = 16 EDIT: Just in case you also wonder how you can change an arbitrary amount of values in an arbitrary row/column: myArray = np.zeros((6,6)) Now...

Storing columns on disk and reading rows

c++,file,matrix,io

Mentioned solution with fseek is good. However, it can be very slow for large matrices (as disks don't like random access, especially very far away). To speed up things, you should use blocking. I'll show a basic concept, and can explain it further if you need. First, you split your...

Modify a matrix in ocaml

matrix,ocaml

In an expression the = operator in OCaml is a comparison operator that tests for equality. To assign to an array, use the <- operator. The compiler is complaining because your expression has type bool (i.e., the result of the comparison). The expression in a for should have type unit...

Async await usage for MongoDB repository

c#,mongodb,asynchronous,parallel-processing,async-await

When you call an async method you should await the returned task, which you can only do in an async method, etc. Awaiting the task makes sure you continue execution only after the operation completed, otherwise the operation and the code after it would run concurrently. So your code should...

How to get node.js to connect to mongolab using mongoose

database,node.js,mongodb,mongoose,mongolab

Try using db = mongoose.connect(uri); instead of db = mongoose.createConnection(uri); ...

Numpy and dot products of multiple vector pairs: how can it be done?

python,numpy,matrix,scipy

np.einsum would do it: np.einsum('ij,ij->i', a_vec, b_vec) ...

Getting failed to load c++ bson extension error using Mongodb and Node.js

javascript,node.js,mongodb

Since you're using the mongojs module, you're going to have to connect to the database using the following method db = mongo("127.0.0.1:27017/"+db, collections); ...

Unable to convert between 1d and 2d array values

java,arrays,matrix,2d

All arrays start by 0, try this: public static void main(String[] args) { int testSize = 4; Test test = new Test(testSize); for (int i = 0; i < testSize; i++) { for (int j = 0; j < testSize; j++) { int index = test.toIndex(i, j); int coordinates[] =...

What are some patterns I can look at for database implementations in JavaScript?

javascript,node.js,mongodb

That's a pretty wide question, partly opinion based. This question should be closed, but I still want to give you some advice. There once was the Active Record pattern, which has been proven to be pretty difficult to maintain. The solution was the DAO pattern, but this adds a lot...

Add a matrix of 2x2 into a vector in c++

c++,matrix,vector

You cannot push back a matrix into a vector. What you can do is preallocate memory for your vector (for speeding things up) then use the std::vector<>::assign member function to "copy" from the matrix into the vector: vector<int> collectionSum(gray.rows * gray.cols); // reserve memory, faster collectionSum.assign(*auxMat, *auxMat + gray.rows *...

Create a Triangular Matrix from a Vector performing sequential operations

r,for-loop,matrix,vector,conditional

Using sapply and ifelse : sapply(head(vv[vv>0],-1),function(y)ifelse(vv-y>0,vv-y,NA)) You loop over the positive values (you should also remove the last element), then you extract each value from the original vector. I used ifelse to replace negative values. # [,1] [,2] [,3] [,4] [,5] # [1,] NA NA NA NA NA # [2,]...

Inserting One Row Each Time in a Sequence from Matrix into Another Matrix After Every nth Row in Matlab

arrays,matlab,matrix

Here's another indexing-based approach: n = 10; C = [A; B]; [~, ind] = sort([1:size(A,1) n*(1:size(B,1))+.5]); C = C(ind,:); ...

Find Mongoid geospacial circles that contain a point

mongodb,mongoid,geospatial

Modelling a "circle" is a valid approach by specifying a "centre" and a "radius", but finding things "within the circle" is generally straightforward if not immediately obvious: Service.collection.aggregate([ # Get the distance from the current location { "$geoNear" => { "near" => { "type" => "Point", "coordinates" => [ 1,...

mongodb aggregate unwind array and no array

arrays,mongodb,aggregation-framework

This basically does what you want with some help from $cond and $ifNull: db.collection.aggregate([ { "$project": { "x": 1, "y": { "$cond": [ { "$ifNull": [ "$y.0", null] }, "$y", { "$map": { "input": ["A"], "as": "el", "in": "$y" }} ] } }} ]) So those first conditions work out...

How to add a subdocument in a mongoose schema

javascript,node.js,mongodb,mongoose

Since the address field is not required by Mongoose when you retrieve the Model from the database the field just won't be defined. Thus you won't be able to add an address field. You should check to see if it exists if(member.address === undefined){ member.address.push(new_address); } else{ member.address = [new_address];...

Cassandra data model to store embedded documents

mongodb,database-design,cassandra

If your document nesting level is not too deep, you can use User Defined Types from C* 2.1. I personally suggest to rethink your schema into more flat form like: create table profiles ( name text, name2 text, email text, username text, ts timestamp, primary key (name,name2) // compound primary...

Why the query scans 8x times the documents it finds - MongoDB

mongodb

It's not "documents" it's "Objects" as is stated, but you would not be the first person to not fully understand the .explain() output. Put simply, you have as part of your index an "array" element (actually the maximum "two" allowed), which means that your index is what we call "MultiKey"....

MongoJS - No Error On Unique Index

mongodb,mongojs

This seems to be a "buggy" response from mongojs which likely results from it's implementation of returning the "inserted" object as a response. The "core" driver does not do this but would either return a "ok" response or an error: var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost/test', function(err,db) { var collection =...

How could I get the matched nested items in array

mongodb

You should first $unwind records and then match elements like following: db.collection.aggregate({ "$unwind": "$records" }, { "$match": { "records.items": { "$in": ["A428 ", "A429 "] } } }, { "$group": { "_id": "$_id", "records": { "$push": "$records" } } }).pretty() ...

Meteor/MongoDB limiting the result

mongodb,meteor

Meteor's collection API is somewhat different from that of the mongo API. find takes up to two parameters: a selector object, and an options object. options allows you to specify such things as sort, skip, limit and fields, in addition to the meteor-specific reactive and transform.

mongodb populate method not working

node.js,mongodb,model,populate,auto-populate

When you create an instance of the Post model, you need to assign the _id from the user as an ObjectId, not a string: var ObjectId = require('mongoose').Types.ObjectId; Post.create({ text: 'farheen123', created_by: new ObjectId('5587bb520462367a17f242d2') }, function(err, post) { if(err) console.log("Farheen has got error"+err); else console.log(post); }); ...

How to check if multiple documents exist

mongodb,mongodb-query

You are looking for the $in-operator. db.collection.find({ id: { $in: { [ 1, 3 ] } }); This will get you any documents where the id-field (different from the special _id field) is 1 or 3. When you only want the values of the id field and not the whole...

Creating a matrix based on a function in R

r,matrix,data.frame

Is that what you want? 1-cov2cor(A) [,1] [,2] [,3] [,4] [,5] [1,] 0.0000000 0.5232687 0.6348516 0.7000000 0.8174258 [2,] 0.5232687 0.0000000 0.5648059 0.6186150 0.7388835 [3,] 0.6348516 0.5648059 0.0000000 0.5435645 0.5000000 [4,] 0.7000000 0.6186150 0.5435645 0.0000000 0.8174258 [5,] 0.8174258 0.7388835 0.5000000 0.8174258 0.0000000 ...

find indices x,y of a matrix of specific values in python

python,matrix,multidimensional-array,indices

You need to change your where line to something like: data_indices = numpy.where((data<=obj_value_max) & (data>=obj_value_min)) Notice the ()s around each conditional clause and the use of & (meaning "and"). This works because in numpy, <,<=,>,>=,&,|,... are overridden, i.e. they act differently than in native python. and and or cannot be...

Creating index while updating the documents

mongodb,indexing

The amount of time it takes to add the index would depend on your hardware, but with 20206 records a simple index as you describe shouldn't take very long for most hardware. Queries fully covered by the index (i.e. where you specify A and B, or just A, but not...

Do you get the same performance using index prefixes?

performance,mongodb,indexing

A "compound index" which is the correct term for your "link" does not create any performance problems on "read" ( since writing new entries is obviously more information ) than an index just on the single field used in the query. With one exception. If you use a "multi-Key" index...

AngularJS factory dependencies

javascript,html,angularjs,mongodb

DOCS Change your factory to service as you are using this factory return an object or primitive type not bind with this app.services('articleFactory', ['$q', '$http', function ($q, $http){ this.getAllArticles = function (){ var deferred = $q.defer(), httpPromise = $http.get ('/entries'); httpPromise.success (function (data){ deferred.resolve (data); }) .error (function (err){ console.log...

How to overwrite object Id's in Mongo db while creating an App in Sails

mongodb,sails-mongo

Attention: Mongo id should be unique as possible in order to scale well. The default ObjectId is consist of a timestamp, machine ID, process ID and a random incrementing value. Leaving it with only the latter would make it collision prone. However, sometimes you badly want to prettify the never-ending...

Inserting a variable in MongoDB specifying _id field

python,mongodb,pymongo

Insert only accepts a final document or an array of documents, and an optional object which contains additional options for the collection. db.collection.insert( <document or array of documents>, { // options writeConcern: <document>, ordered: <boolean> } ) You may want to add the _id to the document in advance, but...

Matlab - Multiply specific entries by a scalar in multidimensional matrix

matlab,matrix,multidimensional-array,scalar

Errr, why you multiply indexes instead of values? I tried this: comDatabe(:,:,[1 2 3],:,8) = comDatabe(:,:,[1 2 3],:,8)*-1 And it worked....

How to use a variable as an Object Key [MongoDB] [duplicate]

node.js,mongodb

JavaScript has no nice notation for creating an object where the keys are variables: var $push_query = {}; $push_query[name] = item; ... {"$push": $push_query} ... ...

I can't fix camera to any object in a 2D Matrix

c#,matrix,camera,xna,viewport

The reason you are getting NullReferenceException is because GraphicsDevice is not initialized yet. Instead of creating your Camera in the TheGame constructor, create it in the Initialize method: protected override void Initialize() { base.Initialize(); camera = new Camera(GraphicsDevice); } How to make camera fixed on any object in the Matrix...

Query with filter builder on nested array using MongoDB C# driver

c#,mongodb,mongodb-query,mongodb-csharp,mongodb-csharp-2.0

The query you need to perform uses the $elemMatch query operator. So, this query using a lambda expression var findFluent = collection.Find(f => f.Bars.Any(fb => fb.BarId == "123")); Is equivalent to this query using the FilterDefinitionBuilder: var findFluent = collection.Find(Builders<Foo>.Filter.ElemMatch( foo => foo.Bars, foobar => foobar.BarId == "123")); ...

Incorrect response to mapReduce query in mongo-db

mongodb,mapreduce

Your problem here is that you have missed one of the core concepts of how mapReduce works. The relevant documentation that explains this is found here: MongoDB can invoke the reduce function more than once for the same key. In this case, the previous output from the reduce function for...

Compare Array with Collection-Array containing Objects

node.js,mongodb,mongoose,schema

You can try this: objectModel.find({ 'members.user_id' : {'$in' : ['asdf123lkd', 'asdf1223']} }, function(err, data) { console.log(err,data); }) ...

How to solve for matrix in Matlab?

matlab,matrix,least-squares

Rewrite the quantity to minimise as ||Xa - b||^2 = (definition of the Frobenius norm) Tr{(Xa - b) (Xa - b)'} = (expand matrix-product expression) Tr{Xaa'X' - ba'X' - Xab' + bb'} = (linearity of the trace operator) Tr{Xaa'X'} - Tr{ba'X'} - Tr{Xab'} + Tr{bb'} = (trace of transpose of...

MongoDB Java Driver 3.0 MapReduce

mongodb,mongodb-java

Yes, this is the expected behavior. Since the MapReduceIterable is a fluent interface, there must be some way to signal the driver that it's time to actually do the map-reduce, and currently the only way to do that is to start iterating. If you really don't need the results, and...

paging subdocument in mongodb subdocument

mongodb,mongodb-query

Though this is possible to do with some real wrangling you would be best off changing the document structure to "flatten" the array entries into a single array. The main reason for this is "updates" which are not atomically supported by MongoDB with respect to updating the "inner" array due...

Query an array of embedded documents in mongodb

mongodb,embedded-documents

You can push the item into the array with the following command: db.mycollection.update({ _id: "zinfandel", "last_search.engine": { $nin: ["notwellknownengine.com"] } }, { $push: { "last_search": { "engine" : "notwellknownengine.com", "query" : "stackoveflow.com" } } }); ...