How to return popped element in mongodb - mongodb

I would like to pop one item from the array, and get that element.
Is it possible in mongodb?
I didn't find such information at
http://docs.mongodb.org/manual/reference/operator/pop/

This is not possible. MongoDB does not return any part of the document you previously had using a normal update, that being said it can return the full document, not just that popped element on findAndModify ( http://docs.mongodb.org/manual/reference/command/findAndModify/ ), you could then filter that element out, either the first or the last.
NB: I should warn that findAndModify is basically like picking out every document in the query and operating on it, that's how it is able to return it so you might see some performance loss on queries that span a large nummber.

MongoDB operations return full documents.
Options could be to:
Use the findAndModify command and get the popped value of the array program side by processing the returned document. (1 query)
use find to get the document, get the value you want to pop on program side, and send the update. (2 queries)

Related

MongoDB Compass display fields in doc by order

Looking for a way to view the results of a query where the displayed fields in the doc are ordered (lexicographically in my case).
Example:
I'm getting back from a query one document, which is what I need. This document has 30 fields and I'm looking to see the value in one of them. My issue is that the order of the fields is, well, kinda random. Not sorted in any way I'm aware of.

Using github.com/icza/minquery to directly query page 3 value

I wanna confirm the right way to get skip(3) values using minquery, 1. foreach skip, get 1,2,3 page data, then return the 3rd value? or 2. use a way to get the cursor of skip(3). if the 2rd is right, how to get the cursor of skip(3) page? Thanks.
You can't skip documents directly using github.com/icza/minquery. The purpose of minquery is to not have to use Query.Skip() (because that becomes less efficient when the number of "skippable" documents grows). The only way to skip 3 documents is to query for more than 3, and throw away the first 3.
minquery is for cases where you don't have to skip the initial documents. minquery requires you to iterate over the documents, and acquire the cursor that encodes the index entry of the last returned document (this cursor is returned to you by MinQuery.All()). When you need the next page, you have to use the cursor you acquired in the previous query, and then it can list subsequent documents without having to skip anything, because the encoded index entry can be used to jump right where the last query finished listing documents.
Think of GMail: you can always jump just to the next (and previous) page of emails, but you have no way of "magically" jumping to the 10th or 100th page: GMail uses the same mechanism under the hood.

Meteor Mongodb first object array not selectable with dot notation

I have tried and tried on Meteor and on Robomongo (Mongodb) to select objects with dot notation.
I would like to be able to filter team.0.wageringStats.wageringStraightSpread objects (sometimes subjects can be fields or arrays - thats another issue)
In the first image I can select team.wageringStats.wageringStraightSpread and get back all the subOjects of team (team has siblings not shown in images)
The second image I tried team.0.wageringStats.wageringStraightSpread and I get no fields.
Lastly i tried team.[0].wageringStats.wageringStraightSpread and
team[0].wageringStats.wageringStraightSpread and get the same result : 0 fields
I am at a loss and would like some help. Thank you
I am not sure what you are trying to do now? Because in your first command, you already have a list of team that match your criteria and then, put it into the loop of meteor to process. Why do you need to find only the first one ? By the way, in order to select the nth of the result set in mongodb, you will need something like skip and limit
db.collections.find({'team.wageringStats.wageringStraightSpread':1}).limit(1).skip(0)
(in skip, you need to pass the offset you need to reach to)
Also, if you only care about the first one, findOne is the one you need to do the query
db.collections.findOne({'team.wageringStats.wageringStraightSpread':1})
Be aware that the syntax of mongodb and meteor for querying is a bit different

MongoDB $in not only one result in case of repeated elements

I need to get the users whose ids are contained in an array. For this i'm using the $in operator, however being this inside an aggregate operation, i'd like to get back a specific user all the time it's id is present in the array, not just one. For example:
The ids array is A=[a,b,c,b] and U(x) is user with id x
with users.find({_id:{$in:A}}) i get these users as result: U(a),U(b),U(c)
instead i'd like to get back the result: U(a),U(b),U(c),U(b)
so get the user back every time it's id appears.
I understand that $in is working as expected but does anyone have an idea on how can i achieve this?
Thanks
This isn't possible using a MongoDB query.
MongoDB's query engine iterates over the documents in a collection (or over an index if there's a useful one) and returns to you any documents that match your query, in the order it finds them. Whether b appears once, twice, or a hundred times in your query makes no difference: the document with _id of b matches the query and is returned once, when MongoDB finds it.
You can do a post-processing step in your programming language to repeat documents as many times as you want.

MongoDB Match 2 criteria

Given the Set in my Image Example,
Is it possible to retrieve by ID and if 'element is in the array'?
As you can see I can find all the records with the element in the array, but as I drill down and find the one by ID as well, it throws an Error, I suspect my syntax is wrong
here is a link to my chat on SO https://chat.stackoverflow.com/transcript/message/5006697#5006697
Thanks in Advance
CAM
You basically have the $in in the projection field (what you want to return) because you closed out the criteria document (what you want to find) early. Try this:
db.users.find( {'_id':1, 'companies-visited': { $in:["stocktwits"]}})