Mongomapper Association Skips Duplicates

Mongoid Create Embedded Document Inside Parent

From documentation:

Model class name cannot end with "s", because it will be considered as
the pluralized form of the word. For example Status would be
considered as the plural form of Statu, which will cause a few known
problems.

So try to define your relation with the class_name metadata forcing mongoid to use that Class.

embeds_one :status, as: :statusable, class_name: "Status"

With MongoMapper, how can I find records where an ID doesn't exist in another table?

The Mongo snippet you posted is two queries. The MongoMapper equivalent is:

classy_users_ids = MyClass.fields(:user_id).find_each.map(&:user_id).uniq
classless_users = User.where(:id.nin => classy_users_ids)

If you have a lot of users, the first query might be more efficient if you skip the conversion to MongoMapper::Document's with the following:

classy_users_ids = MyClass.collection.distinct(:user_id)

FindAndUpdate vs Update in mongodb

The difference is that FindAndModify() returns the document, either the pre-update or post-update version, together with the update, in one atomic operation. Update is atomic but does not return the document, so if you then query for it it's possible it will have been changed by another process in the interim.

When modifying a single document, both findAndModify() and the
update() method atomically update the document.

Note that this is for a single document - update can modify multiple documents, findandmodify cannot.

Also, findandmodify() can remove a document, update() cannot.

http://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/

How to make ng-repeat filter out duplicate results

You could use the unique filter from AngularUI (source code available here: AngularUI unique filter) and use it directly in the ng-options (or ng-repeat).

<select ng-model="orderProp" ng-options="place.category for place in places | unique:'category'">
<option value="0">Default</option>
// unique options from the categories
</select>

FindAndUpdate vs Update in mongodb

The difference is that FindAndModify() returns the document, either the pre-update or post-update version, together with the update, in one atomic operation. Update is atomic but does not return the document, so if you then query for it it's possible it will have been changed by another process in the interim.

When modifying a single document, both findAndModify() and the
update() method atomically update the document.

Note that this is for a single document - update can modify multiple documents, findandmodify cannot.

Also, findandmodify() can remove a document, update() cannot.

http://docs.mongodb.org/manual/reference/method/db.collection.findAndModify/

map through array and return all matches mongodb

You can do it on the server using $filter
The query bellow fitlers the orders array,and keep only the orders that
have placeId=2

Data in

{
"_id": 1,
"orders": [
{
"foodId": 1,
"placeId": 2,
"qty": 3,
"price": 4
},
{
"foodId": 5,
"placeId": 6,
"qty": 7,
"price": 8
}
]
}

Query

aggregate(
[
{
"$addFields": {
"orders": {
"$filter": {
"input": "$orders",
"as": "order",
"cond": {
"$eq": [
"$$order.placeId",
2
]
}
}
}
}
}
])

Results

{
"_id": 1,
"orders": [
{
"foodId": 1,
"placeId": 2,
"qty": 3,
"price": 4
}
]
}

How to store Date field as ISODate() using jackson in MongoDb

What you need is the Jackson Joda Module. If you import that into your classpath, you can do the following on your mapper to write it as your desired Timestamp:

ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new JodaModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, true);
mapper.writeValueAsString(date);

You can replace date in the code sample above with your POJO as necessary.

Edit:
It looks like what you really want is a custom serializer. That would look something like this:

public class IsoDateSerializer extends JsonSerializer<DateTime> {
@Override
public void serialize(DateTime value, JsonGenerator jgen, SerializerProvider provider) {
String isoDate = ISODateTimeFormat.dateTime().print(value);
jgen.writeRaw("ISODATE(\"" + isoDate + "\")");
}

Then you'll either register it on the mapper for all DateTime types

mapper.addSerializer(DateTime.class, new IsoDateSerializer());

or specify it on the function using annotations

@JsonSerializer(using = IsoDateSerializer.class)
public DateTime createdTime;

MongoDB / Morphia saves technical id as ObjectId although it's a String in Java

not sure if this is solved yet. I had the same problem. The solution for me was to set the id myself.

@Id
private String id = new ObjectId().toString();

Now you can treat the id field like any other string field.

Hope this helps.



Related Topics



Leave a reply



Submit